TheTiredEngineer
New Member
- Joined
- Jul 31, 2024
- Messages
- 42
- Office Version
- 365
- Platform
- Windows
Hi All,
Im trying to create a macro that sets defaults on the "Main" sheet (or any sheet) using values from the "Defaults" sheet. Ive got screenshots below of a sample workbook as I cant share any screenshots of the actual workbook.
What the macro needs to do is search in the defaults sheet, increment by row to grab a variable name from each cell in column B, the default value in Column C and the sheet name given in Column F. In the actual workbook, I have 30 sheets to choose from, with about 700 named variables, which are broken up into sections based on the sheet name. Then using those values it found, it needs to find the sheet and named cell and set the value of the named cell to the value it grabbed from the defaults sheet. I hope I explained it ok. I know I need to use Find/Replace to do this but Im not really sure where or how to create this.
Actual data in the defaults sheet starts on row 3 and every so often has to skip a couple of rows of non-data to move on.
Heres some code I started with. It seems to be grabbing the variable name correctly but I dont know where to go from here. It doesnt get into the For loop for whatever reason. Im very unfamiliar with this advanced coding, so any help is very appreciated.
Im trying to create a macro that sets defaults on the "Main" sheet (or any sheet) using values from the "Defaults" sheet. Ive got screenshots below of a sample workbook as I cant share any screenshots of the actual workbook.
What the macro needs to do is search in the defaults sheet, increment by row to grab a variable name from each cell in column B, the default value in Column C and the sheet name given in Column F. In the actual workbook, I have 30 sheets to choose from, with about 700 named variables, which are broken up into sections based on the sheet name. Then using those values it found, it needs to find the sheet and named cell and set the value of the named cell to the value it grabbed from the defaults sheet. I hope I explained it ok. I know I need to use Find/Replace to do this but Im not really sure where or how to create this.
Actual data in the defaults sheet starts on row 3 and every so often has to skip a couple of rows of non-data to move on.
VBA Code:
Option Explicit
Dim i As Integer
Dim n As Integer
Sub ResetToDefaults()
Dim c As Range
Dim firstAddress As String
With ThisWorkbook
For i = 3 To Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
Dim lookupvalue As String
lookupvalue = Sheets(1).Cells(i, 1).Value
Debug.Print lookupvalue & vbCrLf
For n = 1 To Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
If TypeName(Application.Search(lookupvalue, Sheets(2).Cells(n, 1))) = "VarA" Then
Sheets(2).Cells(n, 1).Value = Sheets(1).Cells(i, 2).Value
GoTo exitloop
End If
Next n
exitloop:
Next i
End With
End Sub
Heres some code I started with. It seems to be grabbing the variable name correctly but I dont know where to go from here. It doesnt get into the For loop for whatever reason. Im very unfamiliar with this advanced coding, so any help is very appreciated.