Run a VBA macro to trigger macros in different sheets - excel

The situation is like this:
I have 40 worksheets embedded with same Macros called RetrieveNumbers. The end results in 40 sheets will be different based upon various parameters in each worksheet.
To update the numbers, I manually click the macro buttons to retrieve numbers in the 40 worksheets. As a result of that, I'm sick of it. To simplify the testing, I only use two sheets(Sheet1, Sheet2) to test if, by clicking a Macro named RunAll, it would run through the two Macros.
Surely, I have FAILED.
I have tried :
application.run
call
Two scenarios I tried:
I hit F5 as I was in the RunAll window and my other screen on the Sheet1 worksheet. It runs perfectly and yet it runs twice in Sheet1 rather than going to Sheet2.
I hit F5 as I was in the RunAll window and my another screen on the RunAll worksheet. After clicking it, I went back to see if there were any numbers. And surely, there weren't.
I thought the Macro would go to Sheet2 and then run Macro Retrivenumbers2. But it didn't. It stayed at the current worksheet. Please give me some guidance on how to run the next sheets I want. Let me know if I need to clarify more on this question.
The Macro RetrieveNumbers
(Since the Macro RetrieveNumbers2 is as same as RetrieveNumbers1, I don't include it)
Sub RetrieveNumbers1()
Dim NumberFiles As Integer, FilesVisited As Integer, RowNumber As Integer
Let NumberFiles = ActiveSheet.Cells("2", "A").Value
Let FilesVisited = 0 'start from 0
Let RowNumber = 4 'start from column B
If NumberFiles > 30 Then
MsgBox "Don't try to retrieve numbers from more than 30 files at a time!"
Else
For FilesVisited = 1 To NumberFiles
'Open files, get path, file, tab name and cells
Dim PathFileOpen As String, NameFileOpen As String, NameTab As String, FileDir As String
Let PathFileOpen = ActiveSheet.Cells(RowNumber, "A").Text
Let NameFileOpen = ActiveSheet.Cells(RowNumber, "B").Text
Let NameTab = ActiveSheet.Cells(RowNumber, "C").Text
Dim N As Integer, Cell As String, NumberYears As String, FullLink As String
NumberYears = ActiveSheet.Cells("2", "B").Value
For N = 4 To NumberYears + 3
Cell = ActiveSheet.Cells(RowNumber, N).Text
FullLink = "(=)'" & PathFileOpen & "\[" & NameFileOpen & ".xlsm]" & NameTab & "'!" & Cell
ActiveSheet.Cells(RowNumber, N + 13).Value = FullLink
Next N
RowNumber = RowNumber + 1
Next FilesVisited
End If
ActiveSheet.Range("A1").CurrentRegion.Replace What:="(=)", Replacement:="=", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, ReplaceFormat:=False
End Sub
The Macro RunAll
Sub runall()
Call Sheet1.RetrieveNumbers1
Call Sheet2.RetrieveNumbers2
End Sub
Clear Example of the file
Working file example

There's a fair amount wrong with your code. As #PGCodeRider said in his answer - have one procedure that runs on all sheets. His code has the loop within the procedure.
This code uses a separate procedure to cycle through the sheets and passes a reference to the sheet to the RetrieveNumbers procedure.
I've replaced all instances of ActiveSheet (reference to the ActiveSheet) with wrkSht (reference to the sheet that the RunAllSheets procedure passes).
All Dims have been moved to the top of the code as they only need declaring once and not on each loop (you change the value the variables hold on each loop, but no need to declare them again).
Sub RunOnAllSheets()
Dim wrkSht As Worksheet
For Each wrkSht In ThisWorkbook.Worksheets
Select Case wrkSht.Name
Case "Sheet1", "Sheet2"
'Do nothing.
Case Else
'For all other sheets execute the RetrieveNumbers procedure
'and pass the wrkSht variable to it.
RetrieveNumbers wrkSht
End Select
Next wrkSht
End Sub
Sub RetrieveNumbers(wrkSht As Worksheet)
Dim NumberFiles As Integer, FilesVisited As Integer, RowNumber As Integer
'You only need to declare these once.
Dim PathFileOpen As String, NameFileOpen As String, NameTab As String, FileDir As String
Dim N As Integer, Cell As String, NumberYears As String, FullLink As String
'No need to use 'LET' it's a left-over from the days of Sinclair Basic
'ok, maybe not.... but it's an old way of doing it.
NumberFiles = wrkSht.Cells("2", "A").Value
FilesVisited = 0 'start from 0
RowNumber = 4 'start from column B
If NumberFiles > 30 Then
MsgBox "Don't try to retrieve numbers from more than 30 files at a time!"
Else
For FilesVisited = 1 To NumberFiles
'Open files, get path, file, tab name and cells
PathFileOpen = wrkSht.Cells(RowNumber, "A").Text
NameFileOpen = wrkSht.Cells(RowNumber, "B").Text
NameTab = wrkSht.Cells(RowNumber, "C").Text
NumberYears = wrkSht.Cells("2", "B").Value
For N = 4 To NumberYears + 3
Cell = wrkSht.Cells(RowNumber, N).Text
FullLink = "(=)'" & PathFileOpen & "\[" & NameFileOpen & ".xlsm]" & NameTab & "'!" & Cell
wrkSht.Cells(RowNumber, N + 13).Value = FullLink
Next N
RowNumber = RowNumber + 1
Next FilesVisited
End If
wrkSht.Range("A1").CurrentRegion.Replace What:="(=)", Replacement:="=", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
End Sub
Edit, after accepted as answer:
This method only references the sheet twice. Once to pull the link info, and once more to put the final formula back on the sheet.
Sub RunOnAllSheets()
Dim wrkSht As Worksheet
For Each wrkSht In ThisWorkbook.Worksheets
'Have removed the Select Case statement so it looks at all sheets.
RetrieveNumbers wrkSht
Next wrkSht
End Sub
Sub RetrieveNumbers(wrkSht As Worksheet)
Dim NumberFiles As Long, FilesVisited As Long
Dim vCellValues As Variant, vLinkValues() As Variant
Dim FullPath As String
Dim x As Long
With wrkSht
'Get the last row number that contains data in column N.
NumberFiles = .Cells(.Rows.Count, "N").End(xlUp).Row
If NumberFiles - 3 > 30 Then
MsgBox "Don't try to retrieve numbers from more than 30 files at a time!"
Else
'Pass the cell values to an array.
vCellValues = .Range("A4:C4")
'Create the full path excluding the cell reference.
FullPath = "='" & vCellValues(1, 1) & "[" & vCellValues(1, 2) & "]" & vCellValues(1, 3) & "'!"
'Create an array of full path & cell references.
ReDim vLinkValues(1 To NumberFiles - 3) 'Set the array size.
For x = 1 To NumberFiles - 3
vLinkValues(x) = FullPath & .Cells(x + 3, "N")
Next x
'Paste the array back to the sheet.
.Range(.Cells(4, "N"), .Cells(NumberFiles, "N")).Formula = vLinkValues
End If
End With
End Sub
Note: This assumes your path is just in cell A4:C4, as indicated by the code vCellValues = .Range("A4:C4") (I'm not sure this is the case now).
If your paths are on each row matching the cell values you'll need to:
Change vCellValues = .Range("A4:C4") to
vCellValues = .Range(.Cells(4, 1), .Cells(NumberFiles, 3))
Remove the FullPath='.... line.
Change vLinkValues(x) = FullPath & .Cells(x + 3, "N") to
vLinkValues(x) = "='" & vCellValues(x, 1) & "[" & vCellValues(x, 2) & "]" & vCellValues(x, 3) & "'!" & .Cells(x + 3, "N")

Try running a loop through all of the sheets in the workbook? Also make sure you run this in a module in your vba editor. Not your sheet code.
Sub RetrieveNumbers1()
Dim WS As Worksheet
'loop that goes through all sheets in your workbook. Where you used to have
'activesheet, I changed to ws
For Each WS In ThisWorkbook.Sheets
Dim NumberFiles As Integer, FilesVisited As Integer, RowNumber As Integer
Let NumberFiles = WS.Cells("2", "A").Value
Let FilesVisited = 0 'start from 0
Let RowNumber = 4 'start from column B
If NumberFiles > 30 Then
MsgBox "Don't try to retrieve numbers from more than 30 files at a time!"
Else
For FilesVisited = 1 To NumberFiles
'Open files, get path, file, tab name and cells
Dim PathFileOpen As String, NameFileOpen As String, NameTab As String, FileDir As String
Let PathFileOpen = WS.Cells(RowNumber, "A").Text
Let NameFileOpen = WS.Cells(RowNumber, "B").Text
Let NameTab = WS.Cells(RowNumber, "C").Text
Dim N As Integer, Cell As String, NumberYears As String, FullLink As String
NumberYears = WS.Cells("2", "B").Value
For N = 4 To NumberYears + 3
Cell = WS.Cells(RowNumber, N).Text
FullLink = "(=)'" & PathFileOpen & "\[" & NameFileOpen & ".xlsm]" & NameTab & "'!" & Cell
WS.Cells(RowNumber, N + 13).Value = FullLink
Next N
RowNumber = RowNumber + 1
Next FilesVisited
End If
ws.Range("A1").CurrentRegion.Replace What:="(=)", Replacement:="=", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, ReplaceFormat:=False
'restarts on the next ws
Next WS
End Sub

Related

How to compare all defined names in two workbooks?

I need to open two workbooks with same defined names, however different ranges, and highlight in one of the workbooks the differences of contents of every named range.
I compare hard coded ranges on one workbook with two sheets using the attached code.
Private Sub HighlightDifferences()
Dim setOne As Range
Dim setTwo As Range
Set setOne = Sheets("Sheet1").Range("Ongoing_Activities")
Set setTwo = Sheets("Sheet1 (2)").Range("Ongoing_Activities")
'REMOVE THE COLOR FILL
setOne.Interior.ColorIndex = xlNone
For Each cellitem In setOne
If Not StrComp(cellitem, cellitem2, vbBinaryCompare) = 0 Then
cellitem.Interior.ColorIndex = 6
End If
For Each cellitem2 In setTwo
If StrComp(cellitem, cellitem2, vbBinaryCompare) = 0 Then
cellitem.Interior.ColorIndex = 0
End If
Next cellitem2
Next cellitem
End Sub
Please, try the next code:
Sub compareNamesTwoWorkbooks()
Dim wb1 As Workbook, wb2 As Workbook, N1 As Name, N2 As Name, i As Long, j As Long
Dim rngN1 As Range, rngN2 As Range, boolFound As Boolean
Set wb1 = Workbooks("first workbook.xlsx")
Set wb2 = Workbooks("second workbook.xlsx")
For Each N1 In wb1.Names
For Each N2 In wb2.Names
If N1.RefersTo = N2.RefersTo Then
Set rngN1 = Application.Evaluate("'[" & wb1.Name & "]" & _
Replace(Replace(N1.RefersTo, "=", ""), "!", "'!"))
Set rngN2 = Application.Evaluate("'[" & wb2.Name & "]" & _
Replace(Replace(N2.RefersTo, "=", ""), "!", "'!"))
rngN1.Interior.ColorIndex = xlNone: 'rngN1.Parent.Activate: Stop
For i = 1 To rngN1.rows.count
For j = 1 To rngN1.Columns.count
If Not StrComp(rngN1.cells(i, j).Value, _
rngN2.cells(i, j).Value, vbBinaryCompare) = 0 Then
rngN1.cells(i, j).Interior.ColorIndex = 6
End If
Next j
Next i
boolFound = True: Exit For
End If
Next N2
If Not boolFound Then Debug.Print "Names """ & N1.Name & _
""" could not be found in workbook """ & wb2.Name
boolFound = False
Next N1
End Sub
Please, take care of using your real names to define wb1 and wb2` workbooks. Of course, they must be open. They can also be open by the program if needed.
After testing, some feedback would be appreciated...

Loop not going further

I'm pretty new to VBA and programming in general.
This is what I'm trying to do:
I have a database starting on row 21 of my worksheet (storing a name, second name, job). I'm trying to use a for loop, to create a new worksheet for every name in the database - for example (1 - William), (2 - John) ...
When we add a new member to our database and run our macro - It should add the a new sheet (but don't change anything to the existing ones), so with other words just skip (1 - William) and (2 - John) but adds (3 - Kera).
So far it's creating the 2 first worksheets, but when I add someone to my database - the new worksheet isn't added.
Could any of you experts help me with this issue, and brake down what I'm doing wrong?
Sub test()
Dim i As Long, LastRow As Long
LastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Dim blnFound As Boolean
blnFound = False
For i = 21 To LastRow
For x = 1 To Worksheets.Count
If Worksheets(x).Name = ((i -21) + 1) & " - " & Worksheets("Database").Cells(i, 3).Value Then
blnFound = True
End If
Next
If blnFound = False Then
Worksheets.Add.Move After:=Sheets(1)
ActiveSheet.Name = ((i - 21) + 1) & " - " & Worksheets("Database").Cells(i, 3).Value
End If
Next i
End Sub
I hope you this will help you:
Sub CreateNewSheetFromRange()
Dim mySht As Worksheet
Dim mySheet As Worksheet
Dim BeginRow As Long
Dim myStr As String
Dim r As New Collection
Dim Pos As Integer
On Error Resume Next
For a = 1 To Sheets.Count
'To List Your Sheet With Name Begin With Number & " - "
'If you don't care how many time time the same value repeat,
'you can disobey this for loop
Pos = InStr(Sheets(a).Name, " - ")
If Pos > 0 Then
r.Add Sheets(a).Name, Mid(Sheets(a).Name, Pos + 3)
End If
On Error GoTo 0
Next
BeginRow = 21
Do While True
On Error Resume Next
myStr = ((BeginRow - 21) + 1) & " - " & Worksheets("Data").Cells(BeginRow, 3).Value
If Worksheets("Data").Cells(BeginRow, 3).Value = "" Then
Exit Do
End If
'If you hope avoid your repeated cells value to create new sheet
'perform this
myVal = r.Item(Worksheets("Data").Cells(BeginRow, 3).Value)
'If you don't care about repeated value, you change this with:
'set mySht=worksheets(myStr)
If Err.Description <> "" Then
On Error GoTo 0
Set mySheet = Sheets.Add(After:=Sheets(Sheets.Count))
'If you don't care about repeated value, remove this r.add
r.Add myStr, Worksheets("Data").Cells(BeginRow, 3).Value
mySheet.Name = myStr
a = 1
End If
BeginRow = BeginRow + 1
Loop
End Sub

Searching multiple tables on the same sheet with the column in varying locations and copying them to a different sheet

Hopefully the title is clear. I am trying to search through multiple tables on a single sheet. The information I am looking for is the same for all of the tables, just that the corresponding column is located in different spots (e.g. in one table the column I want to search is in I, while for another table it could be in O.) which makes it a bit more challenging for me.
I want to search through each column that has the same title (Load Number) and depending on its value, copy that entire row over to a sheet that corresponds with that value.
Below is what I have so far in VBA as well as a picture to hopefully clarify my issue.
Any help/advice is appreciated!
http://imgur.com/a/e9DyH
Sub Load_Number_Finder()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
j = 1
Set ws = Sheets.Add(After:=Sheets("Master"))
ws.Name = ("Test Load " & j)
i = 1
Sheets("Master").Select
For Each cell In Sheets("Master").Range("M:M")
If cell.Value = "1" Then
j = 1
'Set WS = Sheets.Add(After:=Sheets("Master"))
'WS.Name = ("Test Load " & j)
matchRow = cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Test Load " & j).Select
ActiveSheet.Rows(i).Select
ActiveSheet.Paste
Sheets("Master").Select
i = i + 1
ElseIf cell.Value = "" Then
' 2, 3, 4, 5, cases
Else
' Something needs to go here to catch when it doesnt have a load number on it yet
End If
' Err_Execute:
' MsgBox "An error occurred."
Next
End Sub
Try this function. This should work for you. Let me know what the results are with your sheet. I made a mock up sheet and tested it, it worked. I can make changes if this is not exactly what you are looking for.
Option Explicit
Sub copyPaste()
Dim rowCount, row_ix, temp, i As Integer
Dim TD_COL_IX As Integer
Dim td_value As String
Dim td_values() As String
rowCount = Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Row
For row_ix = 1 To rowCount
temp = isNewTable(CInt(row_ix))
If temp > 0 Then
TD_COL_IX = temp
ElseIf TD_COL_IX > 0 Then
td_value = Worksheets("Master").Cells(row_ix, TD_COL_IX)
If Not td_value = "" Then
td_values = Split(td_value, " ")
For i = 0 To UBound(td_values)
If Not sheetExists("Test Load " & td_values(i)) Then
Sheets.Add.Name = "Test Load " & td_values(i)
End If
If Worksheets("Test Load " & td_values(i)).Cells(1, 1).Value = "" Then
Worksheets("Master").Range(Worksheets("Master").Cells(row_ix, 1), Worksheets("Master").Cells(row_ix, TD_COL_IX - 1)).Copy _
Destination:=Worksheets("Test Load " & td_values(i)).Cells(1, 1)
Else
Dim rowCount_pasteSheet As Integer
rowCount_pasteSheet = Worksheets("Test Load " & td_values(i)).Cells(Rows.Count, "A").End(xlUp).Row
Worksheets("Master").Range(Worksheets("Master").Cells(row_ix, 1), Worksheets("Master").Cells(row_ix, TD_COL_IX - 1)).Copy _
Destination:=Worksheets("Test Load " & td_values(i)).Cells(rowCount_pasteSheet + 1, 1)
End If
Next i
End If
End If
Next row_ix
End Sub
Function isNewTable(row_ix As Integer) As Integer
Dim colCount, col_ix As Integer
colCount = Worksheets("Master").Cells(row_ix, Columns.Count).End(xlToLeft).Column
For col_ix = 1 To colCount
If Not IsError(Worksheets("Master").Cells(row_ix, col_ix).Value) Then
If Worksheets("Master").Cells(row_ix, col_ix).Value = "LD #" Then
isNewTable = col_ix
Exit Function
End If
End If
Next col_ix
isNewTable = 0
End Function
' ####################################################
' sheetExists(sheetToFind As String) As Boolean
'
' Returns true if the sheet exists, False otherwise
' ####################################################
Public Function sheetExists(sheetToFind As String) As Boolean
Dim sheet As Worksheet
sheetExists = False
For Each sheet In Worksheets
If sheetToFind = sheet.Name Then
sheetExists = True
Exit Function
End If
Next sheet
End Function

VBA code only deletes row when run in debug mode

Im having trouble deleting Rows when running the code not in debug mode. I put stars next to the line giving me a problem. Works in debug mode but not normally running the code. Any help? I have tried using doevent but in the beginning of the for loop but that didnt work.
Public Sub ItemUpdate(ByVal startRow As Integer, ByVal endRow As Integer, ByVal itemCol As String, ByVal statusCol As String, ByVal manuPNCol As String)
Dim orgSheet As Worksheet
Dim commonSheet As Worksheet
Dim partDesCol As String
Dim partDes As String
Dim vendorColNumber As Integer
Dim vendorColLetter As String
Dim manuPN As String
Dim counter As Integer
Dim replaceRnge As Range
Set orgSheet = ThisWorkbook.ActiveSheet
partDesCol = FindPartDesCol()
Set commonSheet = ThisWorkbook.Worksheets("Common Equipment")
For counter = startRow To endRow
'Get part description value
partDes = Range(partDesCol & counter).Value
'Delete row of empty cells if there is any
If partDes = "" Then
'deleteing empty row
orgSheet.Rows(counter).Delete '************************** Only works in
debug mode.
endRow = endRow - 1
If counter < endRow Then
counter = counter - 1
Else
Exit For
End If
Else
manuPN = Range(manuPNCol & counter).Value
'Search for user part in common sheet
Set rangeFind = commonSheet.Range("1:200").Find(partDes, lookat:=xlWhole)
If rangeFind Is Nothing Or partDes = "" Then
Debug.Print "Part " & partDes & " not found in Common Equipment"
'MsgBox "Part " & partDes & " not found in Common Equipment"
'Now check if manuPN is in common equipment
Set rangeFind = commonSheet.Range("1:200").Find(manuPN, lookat:=xlWhole)
If rangeFind Is Nothing Or partDes = "" Then
Debug.Print "PartNumber " & manuPN & " not found in Common Equipment"
'Now check if vendor value of item is empty
'Get vendor col
vendorCol = FindSearchCol()
If orgSheet.Range(vendorCol & counter).Value = "" Then
'Copy and paste manufact. data to vendor
'converting from letter column to number and visa versa
vendorColNumber = Range(vendorCol & 1).Column
ManuColTemp = vendorColNumber - 2
ManuPNColTemp = vendorColNumber - 1
VendorPNColTemp = vendorColNumber + 1
ManuCol = Split(Cells(1, ManuColTemp).Address(True, False), "$")(0)
manuPNCol = Split(Cells(1, ManuPNColTemp).Address(True, False), "$")(0)
VendorPNCol = Split(Cells(1, VendorPNColTemp).Address(True, False), "$")
(0)
orgSheet.Range(ManuCol & counter & ":" & manuPNCol & counter).Copy Range(vendorCol & counter & ":" & VendorPNCol & counter)
End If
Else
'Copy new data from common equipment and paste in place of old data
'Get value of status
If statusCol <> "error" Then
orderStatus = orgSheet.Range(statusCol & counter).Value
End If
commonSheet.Rows(rangeFind.Row).EntireRow.Copy
orgSheet.Range(itemCol & counter).PasteSpecial xlPasteValues
If statusCol <> "error" Then
orgSheet.Range(statusCol & counter).Value = orderStatus
End If
End If
Else
'Copy new data from common equipment and paste in place of old data
'Get value of status
If statusCol <> "error" Then
orderStatus = orgSheet.Range(statusCol & counter).Value
End If
commonSheet.Rows(rangeFind.Row).EntireRow.Copy
orgSheet.Range(itemCol & counter).PasteSpecial xlPasteValues
If statusCol <> "error" Then
orgSheet.Range(statusCol & counter).Value = orderStatus
End If
End If
End If
Next counter
'call renumber item numbers
Call NumberItems(0, 0, 0, False)
End Sub
Most likely, you need to step backwards through your range. When you step forward, as you are doing, the counter will skip a row whenever you delete a row:
For counter = startRow To endRow
Change to
For counter = endRow To startRow Step -1
Also, you should declare endRow and startRow as data type Long. The range of Integer will not cover all the rows in an Excel worksheet; and also VBA is said to convert Integers to Longs when doing the math anyway.

excel vba step thru rows faster

the code below works 100%. It scans for a match in Column B and copies and renames a group of cells when a match is found. However the is a line For lRow = Sheets("HR-Calc").Cells(Cells.Rows.count, "b").End(xlUp).Row To 7 Step -1
Where the step -1 will scan row by row from the bottom of the sheet until a match is found. It would be much easier if the step was set to End.(xlUp) instead of -1. searching every row is overkill because of how the data is set up End.(xlUp) would massive cut down the run time.
Is something like this possible?
Sub Fill_CB_Calc()
M_Start:
Application.ScreenUpdating = True
Sheets("summary").Activate
d_input = Application.InputBox("select first cell in data column", "Column Data Check", Default:="", Type:=8).Address(ReferenceStyle:=xlA1, RowAbsolute:=True, ColumnAbsolute:=False)
data_col = Left(d_input, InStr(2, d_input, "$") - 1)
data_row = Right(d_input, Len(d_input) - InStr(2, d_input, "$"))
Application.ScreenUpdating = False
Sheets("summary").Activate
Range(d_input).End(xlDown).Select
data_last = ActiveCell.Row
If IsEmpty(Range(data_col & data_row + 1)) = True Then
data_last = data_row
Else
End If
For j = data_row To data_last
CBtype = Sheets("summary").Range(data_col & j)
Sheets("HR-Calc").Activate
For lRow = Sheets("HR-Calc").Cells(Cells.Rows.count, "b").End(xlUp).Row To 7 Step -1
If Sheets("HR-Calc").Cells(lRow, "b") = CBtype Then
CBend = Sheets("HR-Calc").Range("C" & lRow).End(xlDown).Row + 1
Sheets("HR-Calc").Rows(lRow & ":" & CBend).Copy
CBstart = Sheets("HR-Calc").Range("c50000").End(xlUp).Row + 2
ActiveWindow.ScrollRow = CBstart - 8
Sheets("HR-Calc").Range("A" & CBstart).Insert Shift:=xlDown
CBold = Right(Range("c" & CBstart), Len(Range("C" & CBstart)) - 2)
box_name = Sheets("summary").Range(data_col & j).Offset(0, -10)
CBnew = Right(box_name, Len(box_name) - 2) & "-" ' <--this is custom and can be changed based on CB naming structure
If CBnew = "" Or vbCancel Then
End If
CBend2 = Range("c50000").End(xlUp).Row - 2
Range("C" & CBstart + 1 & ":" & "C" & CBend2).Select
Selection.Replace What:=CBold & "-", Replacement:=CBnew, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("C" & CBstart).FormulaR1C1 = "CB" & Left(CBnew, Len(CBnew) - 1)
GoTo M_Start2
Else
End If
Next lRow
M_Start2:
Next j
YN_result = MsgBox("Fill info for another block/inverter?", vbYesNo + vbExclamation)
If YN_result = vbYes Then GoTo M_Start
If YN_result = vbNo Then GoTo jumpout
jumpout:
' Sheets("summary").Range(d_input).Select
Application.ScreenUpdating = True
End Sub
I'm not sure if this will help but I've had a great performance increase with pulling the entire range you need to loop through into a variant array and then looping through the array. If I need to loop through large data sets, this method has worked out well.
Dim varArray as Variant
varArray = Range(....) 'set varArray to the range you're looping through
For y = 1 to uBound(varArray,1) 'loops through rows of the array
'code for each row here
'to loop through individual columns in that row, throw in another loop
For x = 1 to uBound(varArray, 2) 'loop through columns of array
'code here
Next x
Next y
You can also define the column indexes prior to executing the loop. Then you only need to execute the you need to pull those directly in the loop.
'prior to executing the loop, define the column index of what you need to look at
Dim colRevenue as Integer
colRevenue = 5 'or a find function that searches for a header named "Revenue"
Dim varArray as Variant
varArray = Range(....) 'set varArray to the range you're looping through
For y = 1 to uBound(varArray,1) 'loops through rows of the array
tmpRevenue = CDbl(varArray(y, colRevenue))
Next y
Hope this helps.
Look at doing a .find from the bottom up.
Perform a FIND, within vba, from the bottom of a range up
That will eliminate the need to do the for loop from the last row to the first occurrence of the value you want to locate.

Resources