VB 6.0 - AutoSum of variable length lists
Asked By Chris Prior
29-Jun-05 05:27 AM
Hello All,
I am trying to import variable length list data from SAP into excel. The format of the data is always the same, but the length of the list data changes every time.
I can import the data, but am having difficulty summing the column as the list length varies.
I have been examining code similar to the following, but now need some way of positioning the autosum at the bottom of the list every time.......any help would be greatly appreciated.
Sub EnterAutoSum ()
vRowTop = 5
vRowBottom = ActiveCell.Offset(-1, 0).Row
vDiff = vRowBottom - vRowTop + 1
Selection.FormulaR1C1 = "=SUM(R[" & -vDiff & "]C:R[-1]C)"
End Sub
Newbie to VBA.
Chris P.
You could
loop through the cells wit a counter and when you find an empty one, you are at the end of the list. This assumes that the values are contiguous in the spreadsheet. Since the daa is numeric you can also try the IsNumeric function on the cell contents.
Here is a macro to get you going
This macro will work with variable length rows or columns. It does so by the first 2 lines. Play around with it and you'll see how it works and should be able to apply it to your situation.
Sub sort1()
'
' sort Macro
NumRecsdown = Range("A8").End(xlDown).Row
NumRecsacross = Range("K6").End(xlToRight).Column
Range("A8:" & NumRecsacross & NumRecsdown).Select
Selection.Sort Key1:=Range("J8"), Order1:=xlDescending, Header:=xlNo,
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ActiveWindow.ScrollRow = 1
End Sub
Thanks Phillip, but there is a syntax error
Hello Phillip,
Thanks for replying to my question. I opened a new, blank workbook. Opened VB editor and then pasted in your code.
When I tried to compile the code, it threw it out at the following line:-
Selection.Sort Key1:=Range("J8"), Order1:=xlDescending, Header:=xlNo,
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
complaining of the following error:-
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:
Syntax error
---------------------------
OK Help
---------------------------
I have this code inserted as a module, do I need to define anything or should it just work?
Your further help would be appreciated.
Regards,
Chris
Here is the original link to the code and the fix
Sure... Here is the original link to the code and the fix that you need.
http://www.mrexcel.com/archive2/51000/59308.htm
This is the fix that worked for them.
Range("A8").Resize(NumRecsdown -8+1,NumRecsacross -columns("A").column+1) _
.Sort Key1:=Range("J8"), Order1:=xlDescending, Header:=xlNo,
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Reading a specific field from XML
I want to read a specific field from xml file and corresponding to that field value i want to select the data stored in the xml file.
Macros disappear from personal macro workbook? Excel Whenever I record a new macro to my personal macro workbook it works OK and I can see that a new module has been created ok when in the VBA editor, but when I exit excel and re-open the macro doesn't work and the module has disappeared? Can anyone advise why this is happening and how to prevent it ? Many
Workbook code to Module Code Excel Hi All I am working with an existing worksheet macro and I would like it changed to a regular module macro so I can assign it to a button. The macro is split into a workbook macro and a Private sub module macro. I want to be able to step through the
How to call password protect macro Excel Hi guys my workbook have a password protected macro. I'll implement userform, its have ok button. I enter the macro call statement in OK button click event if I click the button, it displays "COMPILE ERROR HIDDEN MODULE MACRO MODULE" how to call the pwd macro? Plz help Advanced thanks Venky Excel Discussions Workbook (1) Macro (1) Venky (1) Plz (1
Delete macro that is not in a module Excel My personal macro workbook has several ancient macros that were converted from Lotus 1-2-3 about 10 years ago. They show up in the workbook as worksheets with the macro code on them. They are not in any of the modules shown in Visual Basic Editor and there are no empty modules. I can delete the worksheet, but the macro name remains in the list when I want to run a macro. I can click on the name in the list of macros, but then the Delete
saving macro from workbook to Personal Macro Workbook Excel I have a macro saved to a workbook, let us call it Data.xls. I would like this macro saved to my personal macro workbook so it can be available anytime without having to open Data.xls so I