Macro From Excel 2003 Doesn't work in Excel 2007 - vba

Recently I've upgraded from Excel 2003 to Excel 2007. Nearly all of the macros work, except for one part of one macro. On this sheet for this file, there are roughly 21 slots that have been sized to have pictures placed in them. Because of the nature of the work, sometimes there are a lot more than 21 pictures to input into the document. Before hand it was just a hassle because you would sometimes forget to copy rows over, and then couldn't resize the images properly.
So, upon inserting the images into the photo sheet and running the macro, if there are 21 or less photos it will simply place all of the photos into the slots and resize them. More or less, this works fine, there's a few things I have to tweak, but generally it's working.
The problem is the case for when there are > 21 photos inserted. The code was to find the last available picture cell and copy and paste the needed rows after it. Excel 2007 is not finding any of those cells. The formatting I copied from a recorded macro, which explains the odd styling choices.
The picture cells look like this:
I figured that perhaps something about how the styles of that box had been changed between 2003 and 2007, so I decided to record another macro to get the "new" formatting. But even with Excel's Find dialog and selecting one of the photo cells for its formatting, it gives me an error of "Excel cannot find the data you are looking for." As expected, there were subtle differences between the two Find Formats retrieved by the macro recorder, but neither of them find the cells like they did in Excel 2003. I'm not particularly sure what to do here; can anyone point me in the right direction of getting this to work like it did previously?
The code is this:
Cells.Find Code, 2007
Dim rng As Range
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "General"
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
With Application.FindFormat.Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.Underline = xlUnderlineStyleNone
.ThemeColor = 2
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Application.FindFormat.Borders(xlLeft)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlRight)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlTop)
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
With Application.FindFormat.Borders(xlBottom)
.LineStyle = xlContinuous
.ColorIndex = 49
.TintAndShade = 0
.Weight = xlThin
End With
Application.FindFormat.Borders(xlDiagonalDown).LineStyle = xlNone
Application.FindFormat.Borders(xlDiagonalUp).LineStyle = xlNone
With Application.FindFormat.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Application.FindFormat.Locked = True
Application.FindFormat.FormulaHidden = False
Set rng = Sheet2.Cells.Find(What:="", After:=Sheet2.Range("A6"), SearchDirection:=xlPrevious, SearchFormat:=True)
If rng Is Nothing Then
Debug.Print "Nothing"
End If
Cells.Find Code, 2003
Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
Dim r As Range
Set r = Range(start_cell)
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "General"
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
With Application.FindFormat.Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With
With Application.FindFormat.Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
With Application.FindFormat.Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 49
End With
Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
, SearchFormat:=True)
End Function
EDIT
Okay, so I figured out that for some reason, the "choose cell formatting" options was too specific. I went through and just manually chose some of the options who's values I could remember.
The code I currently have is, the error comes at the end of the function, and says Run Time Error '91': Object variable or With block variable not set., and highlights the End Function line.
I have checked to see that find_last_picture_cell is being populated with the correct cell (M102), and it is. But the code still gives me an error and I'm not sure why.
Function find_last_picture_cell(Optional start_cell As String = "A6") As Range
Dim r As Range
Set r = Range(start_cell)
Application.FindFormat.Clear
With Application.FindFormat
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.MergeCells = True
End With
With Application.FindFormat.Font
.Subscript = False
.TintAndShade = 0
End With
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.ColorIndex = xlAutomatic
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Application.FindFormat.Locked = True
Set find_last_picture_cell = Cells.Find(What:="", After:=r, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False _
, SearchFormat:=True)
End Function

Related

Excel VBA Range Variable Throws Error 1004 - I'm in

I need your help please. I am new to using ranges as variables, so there maybe something obvious I'm missing but I can't seem to find a solution after a lot of googling.
I am formatting four sheets of data (headings, pretty fill colour, nice borders). They are all pretty much the same, but they have a varying number of columns. To save repetitious code I've written one procedure to do the formatting and another to change the variables and call the formatting code.
sample of the calling code:
' Set Customer detail variables.
varGlobalID = Sheets(varWST1Dockets).Cells(2, 13).Value
varCustomerName = Sheets(varWST1Dockets).Cells(2, 14).Value
' Format Suspended
' Set Variables
varReportHeading = "Suspended Dockets Investigation"
Set rngDataHeadings = Range("B11", "T11")
Range("B1048576").End(xlUp).Select
Set rngDataTable = Range(Selection, "T11")
Range("B1048576").End(xlUp).Select
Set rngData = Range(Selection, "T12")
' Run Format Reports Procedure
Sheets(varWSSuspended).Select
Call FormatReports
sample of formatting code
' Format Data Headings
rngDataHeadings.Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = -4300032
.PatternTintAndShade = 0
End With
With Selection.Font
.ColorIndex = 2
.TintAndShade = 0
.Bold = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Apply Borders
rngDataTable.Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 2
.TintAndShade = 0
.Weight = xlMedium
End With
The code seems to work on the first run of the variables but not the second. Do I need to unload them before resetting? Or am I doing something else stupidly obviously wrong?
Thanks in advance.
Set rngDataHeadings = Range("B11", "T11") references B11:T11 of the ActiveSheet. Selecting another worksheet and try rngDataHeadings.Select will throw an exception Runtime Error '1004' Select method of Range class failed
It's best to avoid Select and Active. You should watch Selecting Cells (Range, Cells, Activecell, End, Offset)
If you have standard tables this will work.
Sub FormatTable(wsWorksheet As Worksheet, HeaderAddress As String)
Dim rDataBody As Range
Dim rHeader As Range
With wsWorksheet
Set rHeader = .Range(HeaderAddress, .Range(HeaderAddress).End(xlToRight))
Set rDataBody = Range(HeaderAddress).CurrentRegion
Set rDataBody = rDataBody.Offset(1).Resize(rDataBody.Rows.Count - 1)
End With
With rHeader.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = -4300032
.PatternTintAndShade = 0
End With
With rHeader.Font
.ColorIndex = 2
.TintAndShade = 0
.Bold = True
End With
With rHeader
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Apply Borders
With rDataBody.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 2
.TintAndShade = 0
.Weight = xlMedium
End With
End Sub
Call it like this
FormatTable Worksheets("Sheet1"), "B11"

Code work properly while pressing F8 and doesn't while pressing F5

I have a code, which create sheets with some formats. I have a problem that formatting doesn't work properly (for example it changes color of different range or it doesn't merge cells).
I thought that maybe I did something in wrong order or something like that so I started to press F8 from the beggining to very end. And while doing it, it made exactly a sheet like I wanted.
My code is quite long coz there is many subs inside so I'll try to write how it works and insert important parts. If it won't be enought, I'll put here the rest of my code.
It takes name of the first project from ResourcesProjects sheet. It is stored in sResourcesProjectName variable (it works properly)
Next, it calls 3 subs which create 3 sheets: ResourceSheet, DesignExecutionSheet and RisksSheet.
All of this subs have code to create and format created sheet.
First sub (ResourceSheet) works properly, formatting is exactly like I wanted.
The problem is with subs DesignExecutionSheet and RisksSheet. It is not formatted well when it goes by F5.
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
Sheets(sDesignSheetName).Activate
Cells.EntireColumn.Hidden = False
Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
Columns("C:C").ColumnWidth = 3
Columns("D:D").ColumnWidth = 25
Rows("8:8").RowHeight = 25
Rows("12:12").RowHeight = 25
Rows("17:17").RowHeight = 25
Range("C8:E8,C12:E12,C17:E17").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").Select
Selection.RowHeight = 20
Range("C9:C10,C13:C15,C18:C20").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Range("C9:E10,C13:E15,C18:E20").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A6").Select
Range("D9:D10,D13:D15,D18:D20").Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Selection.Font.Bold = True
Range("D9").Value = "ASSIGNED TO IT&V:"
Range("D10").Value = "COVERED BY IT&V:"
Range("D13").Value = "EXECUTED:"
Range("D14").Value = "PASSED:"
Range("D15").Value = "FAILED:"
Range("D18").Value = "OPEN:"
Range("D19").Value = "CLOSED:"
Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
with RisksSheet() there is similar problem so I don't put here code from that sub.
After calling this subs, there is only this in code:
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
EDIT:
I edited my code like Luboš Suk suggested. Now the problem is different. By pressing F8 it works again properly, but by pressing F5 there is an error "application defined or object defined error" in line
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
What is more, the error isn't appear when I set a breakpoint in main sub in line
Call DesignExecutionSheet
and press F5 twice.
I also figured out that when I don't multiple ranges (for example only "C8:E8" instead of "C8:E8,C12:E12,C17:E17") it also works.
But I have many ranges inside, the code will be very long if I'll divide all of that ranges.
This is my code:
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
'wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
'Sheets(sDesignSheetName).Activate
With Sheets(sDesignSheetName)
.Cells.EntireColumn.Hidden = False
.Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
.Columns("C:C").ColumnWidth = 3
.Columns("D:D").ColumnWidth = 25
.Rows("8:8").RowHeight = 25
.Rows("12:12").RowHeight = 25
.Rows("17:17").RowHeight = 25
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
.Font.Bold = True
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17").Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Sheets(sDesignSheetName).Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Sheets(sDesignSheetName).Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Sheets(sDesignSheetName).Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Sheets(sDesignSheetName).Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").RowHeight = 20
With Sheets(sDesignSheetName).Range("C9:C10,C13:C15,C18:C20").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalDown).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalUp).LineStyle = xlNone
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideVertical).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideHorizontal).LineStyle = xlNone
' .Range("A6").Select
With Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20")
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20").Font.Bold = True
Sheets(sDesignSheetName).Range("D9").Value = "ASSIGNED TO IT&V:"
Sheets(sDesignSheetName).Range("D10").Value = "COVERED BY IT&V:"
Sheets(sDesignSheetName).Range("D13").Value = "EXECUTED:"
Sheets(sDesignSheetName).Range("D14").Value = "PASSED:"
Sheets(sDesignSheetName).Range("D15").Value = "FAILED:"
Sheets(sDesignSheetName).Range("D18").Value = "OPEN:"
Sheets(sDesignSheetName).Range("D19").Value = "CLOSED:"
Sheets(sDesignSheetName).Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
So your main problem will be here, where you activate worksheet and then working with it.
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
I suggest you to use better aproach in your code. Properly declare variables, and work with sheets and cells like relative objects and AVOID .activate and .select
In short therm use something like this. This approach is much more robust for you and readable.
Sub doSomething()
Dim myCuteSheet As Worksheet
Set myCuteSheet = Sheets("pinkRidignSheet")
With myCuteSheet
.Range(.Cells(1,1),.Cells(5,5)) 'then do something with range
.Cells(15,20) 'do something with cell
.Columns ("F") 'do something with column
End With
End Sub

macro getting bugged while re run

Not able to re run below macro does anyone know why it breaks on file location ?
Also does someone knows how to loop this macro to pick up more sheets in to the workbook?
trying to pick up the sheets from workbook 2 and format them in below manner.
Sub specalign()
'
' specalign Macro
'
'
Workbooks.Open Filename:= _
"C:\\\\Interface Specifications Master v7 7.xlsx"
Sheets("1.1.1").Select
Sheets("1.1.1").Move After:=Workbooks( _
"Interface Specifications Master v7.8.xlsx").Sheets(2)
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
Columns("N:N").Select
Selection.Delete Shift:=xlToLeft
Range("N1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.ClearContents
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1:N1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("Table1[[#Headers],[Spec ID]]").Select
Selection.AutoFilter
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("Table1[[#Headers],[Spec ID]]").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.Name = "Arial"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Range("A3:B3,G3:M3").Select
Range("Table1[[#Headers],[ Conditionality ]]").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("3:3").RowHeight = 108.75
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("A:A").ColumnWidth = 8#
Columns("B:B").ColumnWidth = 8#
Columns("C:C").ColumnWidth = 34#
Columns("D:D").ColumnWidth = 22#
Columns("E:E").ColumnWidth = 17#
Columns("G:G").ColumnWidth = 8#
Columns("H:H").ColumnWidth = 22
Columns("I:I").ColumnWidth = 8#
Columns("J:J").ColumnWidth = 8#
Columns("K:K").ColumnWidth = 8
Columns("L:L").ColumnWidth = 6#
Columns("M:M").ColumnWidth = 10
End Sub
going off of what #nickslash said your filename looks like it's this issue.
however, there are many things you can do improve this code like getting rid of the scrollcolumns and all the selections.
To get the file path you can right click on the file in your folder and select properties. > On the General Tab select location. Then you'll see the path you'll need.
As you learn vba you'll do better than what I've put here. and select files and make it more dynamic (maybe a prompt to select the file you want as jean demonstrates here).
Update below:
Sub specalign()
Dim ws As Worksheet
Dim wb1 As Workbook
Dim wb2 As Workbook
'this is what mine looks like with the directory
Set wb1 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7.8.xlsx") 'target workbook .8 I kept
Set wb2 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7 7.xlsx") 'source workbook
For Each ws In wb2.Worksheets
ws.Copy after:=wb1.Worksheets(2)
Next

How do I sort this Macro out, so it selects the next row automatically

I am making a Movie Database on Excel, I have set it all up. Its working fine, i decided to add a Data entry form which will allow the user to input movie details in a form and automatically using a macro it would then move this data to a separate Worksheet with all my movies in. I have managed to record all this step and it works fine however it overwrites data and only uses the row that I pasted it to which was 'A47'. I now want to know how to edit the code so it changes to the next row if data is already available in this row. Another thing to note is that my macro also formats that selection, so that would need changing too. The formatting basically changes certain cells to be bold and text alignment. I will attach the code so you can see what I'm talking about. Also the code at the end deletes the data in the data entry form so its fresh for another entry.
Sorry I'm new to this all, I have looked around but no one has a similar problem as mine.
Any help would be appreciated.
Thanks
Sub SubmitMovie()
'
' SubmitMovie Macro
'
'
Range("K9,K11,K13,K15,K17,K19,K21").Select
Range("K21").Activate
Selection.Copy
Sheets("MovieList").Select
Range("A74:G74").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("B74").Select
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("D74").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A74:G74").Select
Range("G74").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Sheets("Add New Movie").Select
Range("K9").Select
ActiveCell.FormulaR1C1 = ""
Range("K11").Select
ActiveCell.FormulaR1C1 = ""
Range("K13").Select
ActiveCell.FormulaR1C1 = ""
Range("K15").Select
ActiveCell.FormulaR1C1 = ""
Range("K17").Select
ActiveCell.FormulaR1C1 = ""
Range("K19").Select
ActiveCell.FormulaR1C1 = ""
Range("K21").Select
ActiveCell.FormulaR1C1 = ""
Range("D28").Select
End Sub
Replace this
Range("K9,K11,K13,K15,K17,K19,K21").Select
Range("K21").Activate
Selection.Copy
Sheets("MovieList").Select
Range("A74:G74").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
with
Dim dest as Range
Activesheet.Range("K9,K11,K13,K15,K17,K19,K21").Copy
'find the first non-empty cell in ColA (from bottom up)
Set dest = Sheets("MovieList").Cells(rows.count,1).End(xlUp).offset(1,0)
dest.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:= False, Transpose:=True
Welcome to SO.
Since you are new to VBA you have chosen a good way to start learning more by using the macro recorder, but you have already learned that it has its limitations. It doesn't always do things in the most efficent way.
Some pointers on how to improve the script:
Remove all code that you don't know what it does. Most of it should be self explanatory, but if you don't know what it does, chances are you don't need it, because the macro recorder adds lots of uneccessary stuff.
Avoid using Select to navigate the worksheet. It is very inefficient and will slow down your code: tips on how to avoid using select.
There are lots of questions on SO about finding the last used row in order to know where new data can be saved.
Use Option Explicit at the top of each code module to minimize confusion and errors caused by typos etc. It will force you to explicitly declare all variables used, which is a good thing since VBA otherwise will accept all variable names as new variant-types if they haven't been declared before.
If you get stuck on a specific problem - ask questions on that specific problem.
This previous post should help you see some of the concepts/syntax involved in the solution: Loops & Rows
The bottom line is you've run into an issue that macro recorder cannot get you out of. It would be really beneficial for you to take some time to learn about loops, counts and the Cells() function in VBA. Olle and Tim are spot on...especially Tim's "Set dest =" line.
This link shows a good example of loop syntax and may be a help to you on future problems of a similar nature:
Looping Through Ranges

Preventing reset of VBA variables from excel recompile

Private Sub CommandButton2_Click()
Dim TempVar As Integer
TempVar = NumNodes
NumNodes = NumNodes + 1
TempVar = NumNodes
Debug.Print "NumNodes + 1"
Call Node_Button_Duplication
Call Channel_Selection_Duplication
NumNodes = TempVar
Debug.Print "NumNodes = " & NumNodes 'Debug
Debug.Print "TempVar = " & NumNodes 'Debug
End Sub
Public Sub Channel_Selection_Duplication()
Range("Q8:S8").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("Q8:S8").Select
ActiveCell.FormulaR1C1 = "Channel Usage Selection"
Range("Q8:S52").Select
Range("Q52").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("Q8:S8").Select
Selection.Interior.ColorIndex = 36
End Sub
Public Sub Node_Button_Duplication()
Worksheets("Topology").Shapes("CommandButton1").Select
Selection.Copy
Worksheets("Topology").Paste
Selection.ShapeRange.IncrementLeft 339#
Selection.ShapeRange.IncrementTop -12.75
End Sub
I'm trying to save the value of NumNodes (a global variable) before calling the 2 subroutines (Node_Button_Duplication and Channel_Selection_Duplication), the first subroutine called copies and pastes a command button in a spreadsheet. This, I believe, recompiles the VBA project and reset (all?) global variables.
I have tried to write to a cell and read back the value from the cell, but this did not work (essentially the same ideas as using a temp variable).
The above code, when run, causes both TempVar and NumNodes to be reset to 1 each run. I am wondering what the best way is to save the variable from being reset?
Try this
Option Explicit
Private Sub CommandButton2_Click()
Dim NumNodes as Long
NumNodes = Sheets("Temp").Range("A1").Value
NumNodes = NumNodes + 1
Sheets("Temp").Range("A1").Value = NumNodes
MsgBox "NumNodes = " & NumNodes
Call Node_Button_Duplication
Call Channel_Selection_Duplication
End Sub
Ensure that you have a sheet Called "Temp"
Now try it.