formula - vba macro

russlock

New Member
Joined
Sep 17, 2003
Messages
26
Hi, not sure if anyone can help but i am basically trying to add a fixed percentage to an existing price (Column G) on a multitude of spreadsheets with each one representing each customer.
i did record a macro and inserted the code into a previous script that Alex amended for me from "TheSpreadsheetGuru.com"
however, the amount of rows with be different on every customer as one customer will buy more items than the other so will have more rows. when I did the macro, the customer customer had 138 rows which I can see in the below script, but anyone who has more rows does not get changed and any with less just end up with a zero.

I need the script to just find the last item in column "G" and to amend the price and move on to the next spreadsheet in the folder.

Hope all the above makes sense.

Many thanks for any help offered.

====================

VBA Code:
Sub testIncrease()
'
' testIncrease Macro
'

'

    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.xls*")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'my recorded macro code here
'
' Add 6% to ccp prices
'
'
    Columns("G:G").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("G:G").Select
    Selection.NumberFormat = "General"
    Range("F1").Select
    Selection.AutoFill Destination:=Range("F1:G1"), Type:=xlFillDefault
    Range("F1:G1").Select
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]*(1+6%)"
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G138")
    Range("G2:G138").Select
    Columns("G:G").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Columns("F:F").Select
    Selection.Delete Shift:=xlToLeft
   
    'end of my recorded macro code
            End With
           
                    'Save and Close Workbook
           'Saving the Workbook
ActiveWorkbook.Save
ActiveWorkbook.Close
        
        'Ensure Workbook has closed before moving on to next line of code
          DoEvents
  
        'Get next file name
            xFileName = Dir
           
        Loop
    End If
   
        'Message Box when tasks are completed
      MsgBox "Task Complete!"
  
ResetSettings:
      'Reset Macro Optimization Settings
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
End Sub
 
Try this on a COPY of your folder. Note: comments beginning with ''' are code lines that were either of no use (such as selecting a range, doing nothing, selecting another range) or raised runtime errors for me. You should delete them when finished looking at them. Comments beginning with '' are mine; single ' are yours.
First, make sure I understand - that the number of rows that need to populate in G is based on the number of values in column F. That is decided here
.Cells(Rows.count, "F").End(xlUp).Row)

VBA Code:
Sub testIncrease()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String

Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
     xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
     xFileName = Dir(xFdItem & "*.xls*")
     Do While xFileName <> ""
          Workbooks.Open (xFdItem & xFileName)
               'my recorded macro code here
               ' Add 6% to ccp prices
               With Sheets("Sheet1")
                    .Activate
                    .Columns("G:G").Select
                    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    '''Columns("G:G").Select
                    Selection.NumberFormat = "General"
                    '''.Range("F1").select
                    .Range("F1").AutoFill Destination:=.Range("F1:G1"), Type:=xlFillDefault
                    '''Selection.AutoFill Destination:=Range("F1:G1"), Type:=xlFillDefault
                    '''.Range("F1:G1").Select
                    .Range("G2").Select
                    ActiveCell.FormulaR1C1 = "=RC[-1]*(1+6%)"
                    '''.Range("G2").Select
                    '''Selection.AutoFill Destination:=Range("G2:G138")
                   .Range("G2").AutoFill Destination:=.Range("G2:G" & .Cells(Rows.count, "F").End(xlUp).Row)
                    '''Range("G2").Select
                    '''Range("G2:G138").Select
                    .Columns("G:G").Select
                    Selection.Copy
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
                    .Columns("F:F").Select
                    Selection.Delete Shift:=xlToLeft
                    'end of my recorded macro code
               End With
             
          'Save and Close Workbook
          ActiveWorkbook.Save
          ActiveWorkbook.Close
          
          'Ensure Workbook has closed before moving on to next line of code
          DoEvents
          'Get next file name
          xFileName = Dir
        Loop
        MsgBox "Task Complete!" ''moved because if user cancels dialog, this message would still present.
End If

ResetSettings: ''these were never disabled in this code but I left them in
     'Reset Macro Optimization Settings
     Application.EnableEvents = True
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
     Set xFd = Nothing ''best practice is to destroy object variables
    
End Sub
 
Upvote 0
Solution

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this on a COPY of your folder. Note: comments beginning with ''' are code lines that were either of no use (such as selecting a range, doing nothing, selecting another range) or raised runtime errors for me. You should delete them when finished looking at them. Comments beginning with '' are mine; single ' are yours.
First, make sure I understand - that the number of rows that need to populate in G is based on the number of values in column F. That is decided here
.Cells(Rows.count, "F").End(xlUp).Row)

VBA Code:
Sub testIncrease()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String

Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
     xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
     xFileName = Dir(xFdItem & "*.xls*")
     Do While xFileName <> ""
          Workbooks.Open (xFdItem & xFileName)
               'my recorded macro code here
               ' Add 6% to ccp prices
               With Sheets("Sheet1")
                    .Activate
                    .Columns("G:G").Select
                    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    '''Columns("G:G").Select
                    Selection.NumberFormat = "General"
                    '''.Range("F1").select
                    .Range("F1").AutoFill Destination:=.Range("F1:G1"), Type:=xlFillDefault
                    '''Selection.AutoFill Destination:=Range("F1:G1"), Type:=xlFillDefault
                    '''.Range("F1:G1").Select
                    .Range("G2").Select
                    ActiveCell.FormulaR1C1 = "=RC[-1]*(1+6%)"
                    '''.Range("G2").Select
                    '''Selection.AutoFill Destination:=Range("G2:G138")
                   .Range("G2").AutoFill Destination:=.Range("G2:G" & .Cells(Rows.count, "F").End(xlUp).Row)
                    '''Range("G2").Select
                    '''Range("G2:G138").Select
                    .Columns("G:G").Select
                    Selection.Copy
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
                    .Columns("F:F").Select
                    Selection.Delete Shift:=xlToLeft
                    'end of my recorded macro code
               End With
            
          'Save and Close Workbook
          ActiveWorkbook.Save
          ActiveWorkbook.Close
         
          'Ensure Workbook has closed before moving on to next line of code
          DoEvents
          'Get next file name
          xFileName = Dir
        Loop
        MsgBox "Task Complete!" ''moved because if user cancels dialog, this message would still present.
End If

ResetSettings: ''these were never disabled in this code but I left them in
     'Reset Macro Optimization Settings
     Application.EnableEvents = True
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
     Set xFd = Nothing ''best practice is to destroy object variables
   
End Sub
Works absolutely perfect. Thanks ever so much. I am going to leave all the comments in there as it doesn't impact the script at all and I will be able to learn from the above in many ways. Hours of my life will be saved and if you were local to me I would be buying the beers. Genius.
 
Upvote 0
Glad I was able to help. I will have one for you later! 🍻
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top