Hello gang,
I am fairly good in MS excel, but relatively new to VBA. I use lots of the tricks and tips of this forum. Thanks.
In my office we distributed a template with several tabs and lots of formulas for individuals to fill in and return. After a couple of weeks of using this template we realized it had some formulas erros. We are talking about 100+, so rather than asking the individuals to retype the info into the new template we decided to created a new template (all errors fix) and populate it thru vba. So, now we are trying to transfer only relevant data (no formulas) into the new template.
First we needed to ensure all the tabs in the old file are named according to a predetermine sequence. So we are using a cell.value to determine what sequence (or name) should the tab be before copying the information into the new template.
Here is the code, I thought it is pretty straight forward but in the " For Each " section of the code for reasons beyond my comprehension when is time to rename the tabs, we are only renaming the same tab. the code does not toggle thru the sheets.
Sub Testing()
Dim File01Name As String, File02Name As String, Sht As Worksheet
Dim SitPos As String, SitId As String, SiteNo As Variant
File01Name = ActiveWorkbook.Name 'Setting up Variable with Active Workbook value
File02Name = "TestingSendingKeys.xls"
Windows(File01Name).Activate
'Array to determine sheet sequence
SiteNo = Sheets("Sheet1").Range("B4", Range("b" & Rows.Count).End(xlUp)).Value
'Activate Workbook where all the changes will take effect
Windows(File02Name).Activate
For Each Sht In ActiveWorkbook.Worksheets
If Len(Sht.Range("A5")) < 14 Then GoTo finishing
'Msg to determine what sheet met the criteria
MsgBox Sht.Name
'Extracting variable to be used in the match
SitId = Mid(Sht.Range("A5"), 14, 6)
'Match function to determine the position of the variable SitId in the Array SiteNo
SitPos = Application.Match(SitId, SiteNo, 0)
MsgBox SitId & ", : " & SitPos
' Here is my problem:
' The sheet containing the SitId info should be renamed Depending of the SitPos value.
' Only the first sheet is changing. The procedure is not physically toggleing thru the sheets.
If SitPos = 2 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 3 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 4 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 5 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 6 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 7 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 8 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 9 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 10 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 11 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 12 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 13 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 14 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 15 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 16 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 17 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 18 Then ActiveSheet.Name = "Sheet " & SitPos - 1
If SitPos = 19 Then ActiveSheet.Name = "Sheet " & SitPos - 1
finishing:
Next Sht
End Sub
Any help will be appreciated.
Bookmarks