I've created a macro that uses 5 columns to paste data into SAP GUI using a loop. However, once it gets to row 30 or 40, it stops pasting the data correctly where it pastes the same number. Not sure if I should add something to enhance the code? All responses will be greatly appreciated, thank you!
On Error Resume Next
Dim Application
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If
i = 2
Do Until Cells(i, 1) = ""
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/ctxtGV_MATNR").Text = Cells(i, 1)
session.findById("wnd[0]/usr/ctxtGV_WERKS").Text = Cells(i, 2)
session.findById("wnd[0]/usr/ctxtGV_WERKS").SetFocus
session.findById("wnd[0]/usr/ctxtGV_WERKS").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txtZIP_MM02_STRUCTURE-EISBE_P").Text = Cells(i, 3)
session.findById("wnd[0]/usr/ctxtZIP_MM02_STRUCTURE-EISBE_RD").Text = Cells(i, 4)
session.findById("wnd[0]/usr/ctxtZIP_MM02_STRUCTURE-EISBE_RD").SetFocus
session.findById("wnd[0]/usr/ctxtZIP_MM02_STRUCTURE-EISBE_RD").caretPosition = 10
session.findById("wnd[0]/usr/ctxtZIP_MM02_STRUCTURE-EISBE_RD").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[21]").press
session.findById("wnd[0]/usr/cntlTEXTEDIT/shellcont/shell").Text = Cells(i, 5) + vbCr + "" + vbCr + ""
session.findById("wnd[0]/usr/cntlTEXTEDIT/shellcont/shell").setSelectionIndexes 99, 99
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
End Sub
Problem with your solution is that the control showing table wont let you access basicaly anything further than what is visible (not actually true, another 3 or so rows may be loaded into memory though).
What you need to do is to refresh the table either by using the "Next Page (Page down)" button from the upper bar of SAP GUI or manipulate the scrollbar.
Either way, remember that the row index will reset back to the beginning so you would need to reset your counter too.
You may find it easier to not contain all the code in On Error Resume Next too.
Example for one of the kinds of shell tables:
For i = 0 To session.findById("wnd[0]/shellcont/shell").rowcount - 1
session.findById("wnd[0]/shellcont/shell").firstVisibleRow = i
Next
Related
So I have a macro that updates prices in SAP GUI. It copies cell by cell and does the update order by order, but some times, right after performing the update and saving the order to go with the next line, a prompt appears in SAP where I need to click Yes or No, or Accept / Reject.
My macro then stops as it does not have a line of code to act on it. I do know which line is required, but my question is how to I write a line of code which would be executed if the prompt appears?
Keep in mind that sometimes it pops up and sometimes it does not.
Here is my code:
Public sessioninfo As SAPFEWSELib.GuiSessionInfo
Public Sub fastPFI()
Dim ws As Worksheet
Dim App As SAPFEWSELib.GuiApplication
Dim sor As Long
Dim maxsor As String
'HOEEUBV2 (EUB with scripting)
Set GuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set App = GuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set Con = App.Children(0) 'Get the first system that is currently connected
Set session = Con.Children(0) 'Get the first session (window) on that connection
Set sessioninfo = session.Info
Set ws = Excel.ThisWorkbook.Worksheets("system")
sor = 2
maxsor = ws.Cells(Rows.Count, 1).End(xlUp).Row
'maxsor = 3
Do While sor < maxsor + 1
session.StartTransaction "va02"
'session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/ctxtVBAK-VBELN").Text = Cells(sor, 1)
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[1]").SendVKey 0
session.FindById("wnd[0]").SendVKey 30
session.FindById("wnd[0]").SendVKey 11
session.FindById("wnd[0]/usr/lblRV45S-BSTNK").SetFocus
session.FindById("wnd[0]/usr/lblRV45S-BSTNK").CaretPosition = 18
'session.FindById("wnd[0]").SendVKey 0
sor = sor + 1
Loop
MsgBox "All proformas have been created" & vbNewLine & "Click OK to close file"
' Application.DisplayAlerts = False
'ActiveWorkbook.Close Savechanges:=False
'Application.DisplayAlerts = True
End Sub```
On Error GoTo Next after the line of code which could or not happen solved it.
i have a certain problem with A VBScript when i try to automate a procedure with VBA/VBS macro.
It enters a contract number in VBS and does some standardized changes in VA03, but for some there are some incoplete information, then this pop-up window appears:
I only want my script to click Save and continue in case this happens for a certain contract number.
It did not work with Applications.DisplayAlerts = False, and also i guess it will not work with Error Handler as practically this is not an Error.
It should be with a simple IF statement, but i do not know how i should put the wording.
Can anyone please help, my research in the net got me nowhere :(
Code (Although it is working, i only need a piece which will handle the above stopper):
today = Format(Date, "dd.mm.yyyy")
Application.DisplayAlerts = False
'We declared the variables for the while function in excel
Dim cont As String
Dim row As Integer
Dim rep As String
Dim j As Integer
j = 2
'Those are the commands with which we make SAP available for the VBA code
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set Session = SAPCon.Children(0)
If IsObject(WScript) Then
WScript.ConnectObject Session, "on"
WScript.ConnectObject Application, "on"
End If
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/okcd").Text = "va42"
Session.findById("wnd[0]").sendVKey 0
'We start the loop inside the macro book and give values to our variables
With ThisWorkbook
While Cells(j, 1) <> ""
cont = Cells(j, 1).Value
row = Cells(j, 3).Value
rep = Cells(j, 4).Value
' enter VBS code
'In this part we change the inst to REMV
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = cont
Session.findById("wnd[0]/usr/ctxtVBAK-VBELN").caretPosition = 8
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4426/subSUBSCREEN_TC:SAPMV45A:4908/tblSAPMV45ATCTRL_U_ERF_KONTRAKT/ctxtVBAP-KDMAT[5," & CStr(row) & "]").SetFocus
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4426/subSUBSCREEN_TC:SAPMV45A:4908/tblSAPMV45ATCTRL_U_ERF_KONTRAKT/ctxtVBAP-KDMAT[5," & CStr(row) & "]").caretPosition = 6
Session.findById("wnd[0]").sendVKey 2
'change date
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\03").Select
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").Text = today
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").SetFocus
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\03/ssubSUBSCREEN_BODY:SAPLV45W:4201/ctxtVEDA-VDEMDAT").caretPosition = 10
'change INST to REMV
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP/tabpT\10").Select
Session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\10/ssubSUBSCREEN_BODY:SAPMV45A:4454/ctxtVBAP-KDMAT").Text = rep
'Deletes the first line in Technical Objects and saves the changes
Session.findById("wnd[0]/mbar/menu[3]/menu[9]").Select
Session.findById("wnd[0]/usr/tblSAPLIWOLOBJK_220").getAbsoluteRow(0).Selected = True
Session.findById("wnd[0]/tbar[1]/btn[19]").press
Session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[11]").press
'we make sure the loop goes through every row and then end it
j = j + 1
Wend
End With
End Sub
Best regards,
Mihail
First, I would record the missing piece of vbs program. Then you have to recognize where in the main program this piece must be installed. But I suspect it will happen after saving. The rough structure might look like this:
. . .
Session.findById("wnd[0]/tbar[0]/btn[11]").press
'------------------------------ new ---------------------------
on error resume next
'Suppose that's the missing piece of program
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
on error goto 0
'------------------------------ new ---------------------------
'we make sure the loop goes through every row and then end it
j = j + 1
Wend
. . .
Regards,
ScriptMan
I added the solution proposed by Script man above!
It worked perfectly!
i have a code that do some SAP transactions on the sap then select a whole column from a grid on a sap i am successful till here but i want to copy the column and past it in an excel sheet but i cant do that. i tried to open the context menu to choose copy text but it doesn't work i tried to send CTRL+C stroke from keyboard but also failed. if someone can help me with that i will be very grateful. here is my code:
Sub lolo()
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/btn%_EQUNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/usr/txtSELCOUNT").Text = "1"
session.findById("wnd[0]/usr/txtSELCOUNT").SetFocus
session.findById("wnd[0]/usr/txtSELCOUNT").caretPosition = 14
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, "PTTXT"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "PTTXT"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]").sendVKey 4
session.findById("wnd[2]/usr/lbl[1,23]").SetFocus
session.findById("wnd[2]/usr/lbl[1,23]").caretPosition = 2
session.findById("wnd[2]").sendVKey 2
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, "CNTRC"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "CNTRC"
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "^C"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
End Sub
I expect the OP is no longer interested in the answer but thinking that people are still reading this question...
add a form to your vba project then the following code into your module
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
sString = DataObj.GetText(1)
This pulls the data from your clipboard into sString. You can then pass the string to a cell value.
I had the same problem, depending on the transaction I either got
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[1]/tbar[0]/btn[0]").press
or
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemBytext "Copy Text"
which both didn't copy the column.
What solved my predicament was this handy line:
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectContextMenuItemByPosition "0"
Could you try with this? I've integrated parts of code that I use to extract data from these types of SAP sheets..
Sub lolo()
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
dim t as object
dim arr as variant
dim index1 as long
session.findById("wnd[0]/usr/btn%_EQUNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/usr/txtSELCOUNT").Text = "1"
session.findById("wnd[0]/usr/txtSELCOUNT").SetFocus
session.findById("wnd[0]/usr/txtSELCOUNT").caretPosition = 14
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, "PTTXT"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "PTTXT"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]").sendVKey 4
session.findById("wnd[2]/usr/lbl[1,23]").SetFocus
session.findById("wnd[2]/usr/lbl[1,23]").caretPosition = 2
session.findById("wnd[2]").sendVKey 2
session.findById("wnd[1]/tbar[0]/btn[0]").press
Set t = session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell")
ReDim arr(0 To t.RowCount, 1 To 2)
arr(0, 1) = "Column1"
arr(0, 2) = "Column2"
For index1 = 1 To t.RowCount
If t.VisibleRowCount + t.FirstVisibleRow < index1 Then
t.FirstVisibleRow = index1
End If
arr(index1, 1) = t.GetCellValue(index1 - 1, "CNTRC")
'arr(index1, 2) = t.GetCellValue(index1 - 1, <any other column>)
Next index1
'To print it to excel alter this to fit into your excel sheet:
ThisWorkbook.Sheets(1).Range("A1:A" & UBound(arr) + 1).Offset(0, 0) = arr
end sub
I've written a subroutine in VBA to insert images into a spreadsheet by product sku (the images are named by sku). I've done as much error handling as I can think of and the code works fine in small batches (including the error handling); but when I run this on the entire spreadsheet (2400 lines), it crashes every time it hits a sku where the image doesn't exist, even though these instances are handled correctly if I go through the debugger.
My first thought was that it was just too much for Excel to handle so I tried to add a pause in (maybe a long shot) - this didn't work.
I've pasted the code below - anyone come across this before?
To explain the code further, next to the sku column there are some associated products so the idea is that if the main image doesn't exist, we can loop through the associated products and try to use one of those - if there still aren't any matching images then the main sku should be stored in an array and then placed on a new sheet for reference at the end of the subroutine - all works perfectly when debugging, but crashes when run on the whole file.
Sub InsertPic()
Dim MySht As Worksheet
Dim MyPic As Shape
Dim MyLeft As Single, MyTop As Single
Dim noImage() As String
Dim noImageSheet As Worksheet
'stop screen updating
Application.ScreenUpdating = False
Application.DisplayAlerts = False
masterSpreadsheet = ActiveSheet.Name
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
'define first part of noImage array
k = 0
Set MySht = ActiveSheet
For i = 2 To lastRow
'save every 100 images
shouldISave = i Mod 100
If shouldISave = 0 Then
ActiveWorkbook.Save
End If
'If i Mod 50 = 0 Then
' Application.Wait (Now + TimeValue("0:00:05"))
'End If
MyTop = Rows(Cells(i, 1).Row).Top + 1
MyLeft = Columns(Cells(i, 1).Column).Left + 1
'turn off error checking
On Error Resume Next
'try to define image based on main sku
Set MyPic = MySht.Shapes.AddPicture("W:\Design on 'Pmcsbs' (Y)\Brochure\" & Cells(i, 2) & ".jpg", _
msoFalse, msoTrue, MyLeft, MyTop, -1, -1)
'use fallback images from associated products
'check if MyPic is set
If MyPic Is Nothing Then
'Split associated products into array
scndImage = Split(Cells(i, 3), ",")
'check that array is not empty
On Error Resume Next
isItEmpty = scndImage(0)
If Len(isItEmpty) > 0 Then
j = 0
'loop through array while picture isn't set
Do While MyPic Is Nothing
'try to set the picture with each assoc product code
Set MyPic = MySht.Shapes.AddPicture("W:\Design on 'Pmcsbs' (Y)\Brochure\" & scndImage(j) & ".jpg", _
msoFalse, msoTrue, MyLeft, MyTop, -1, -1)
j = j + 1
'stop the loop when we reach the upper limit of the array
If j = UBound(scndImage) + 1 Then
Exit Do
End If
Loop
End If
End If
'check if picture still doesn't exist after all codes have been tried
If MyPic Is Nothing Then
ReDim Preserve noImage(k) As String
noImage(k) = Cells(i, 2)
k = k + 1
End If
'turn error checking back on
On Error GoTo 0
'only run the below if MyPic exists
If Not MyPic Is Nothing Then
' now resize pic
MyPic.Height = Cells(i, 1).Height - 2
If MyPic.Width > Cells(i, 1).Width Then
MyPic.Width = Cells(i, 1).Width
End If
End If
Set MyPic = Nothing
Next
'if noImage has data in then print that to a new sheet
On Error Resume Next
isItEmpty2 = noImage(0)
noImgLastRow = 1
If Len(isItEmpty2) > 0 Then
'check if no image sheet exists and get lastRow if it does
On Error Resume Next
Set noImageSheet = Sheets("No Images")
On Error GoTo 0
'if it doesn't exist create it
If noImageSheet Is Nothing Then
Sheets.Add.Name = "No Images"
'if it does, update the last row number
Else
noImgLastRow = noImageSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
End If
m = 1
For l = 0 To UBound(noImage)
Sheets("No Images").Cells(noImgLastRow, 1) = noImage(l)
m = m + 1
noImgLastRow = noImgLastRow + 1
Next
End If
Sheets(masterSpreadsheet).Activate
ActiveWorkbook.Save
'email Jack to confirm it has completed
Call Mail_images_spreadsheet_finished
'Update screen again
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Disabling error checking needs to be reset - these instructions come in pairs. In your code, you disable EC twice for each row but only enable it again once. This leads to memory consumptions which builds up and eventually crashes Excel.
I propose the following changes:
' turn off error checking
On Error Resume Next
'try to define image based on main sku
Set MyPic = MySht.Shapes.AddPicture("W:\Design on 'Pmcsbs' (Y)\Brochure\" & Cells(i, 2) & ".jpg", _
msoFalse, msoTrue, MyLeft, MyTop, -1, -1)
On Error GoTo 0 ' <~~ reset EC
...
and again here:
On Error Resume Next
isItEmpty = scndImage(0)
' many many lines of code following
...
'turn error checking back on
On Error GoTo 0
change to:
On Error Resume Next
isItEmpty = scndImage(0)
'turn error checking back on
On Error GoTo 0 ' <~~
' many many lines of code following
...
and
'if noImage has data in then print that to a new sheet
On Error Resume Next
isItEmpty2 = noImage(0)
On Error Goto 0 ' <~~
Then, your code still contains a commonly seen bug when using On Error Resume Next. If you have
On Error Resume Next
set testvar = myarray(indx)
On Error Goto 0
if testvar is Nothing then ' element does not exist
then the set testvar statement will be skipped if the array element doesn't exist. Assume that the test is positive and testvar is set; if the test fails in the next loop iteration if testvar is Nothing will be False although the assignment failed. You have to reset the testvar before trying the assignment!
set testvar = Nothing ' <~~ !!!
On Error Resume Next
set testvar = myarray(indx)
On Error Goto 0
if testvar is Nothing then ' element does not exist
In your code this affects MyPic and isItEmpty and isItEmpty2.
This script is to reset a template, by copying a hidden worksheet template and deleting the existing sheet (after repopulating some reference data). I have tested it and it runs fine in debugging mode.
Option Explicit
Sub reset_PrintLayout_byCopy()
'the script replace the used printlayout with a copy from the hidden master.
Dim MeetingData() As String
Dim i As Integer
Dim j As Integer
Dim currentSheet As String
Dim datacolumns() As String
Dim userConfirm As String
ReDim Preserve MeetingData(3, 2)
ReDim Preserve datacolumns(2)
'warning about deleting data
userConfirm = MsgBox(Prompt:="Resetting the template will erase all data on the " _
& "PrintLayout Template. Choose ""Cancel"", if you wish to save the file first", _
Buttons:=vbOKCancel, Title:="Data to be erased!")
If (userConfirm = vbCancel) Then
Exit Sub
End If
'set parameters
datacolumns(0) = "D1"
datacolumns(1) = "I1"
'stop screen updating and displaying warnings
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'set active sheet
currentSheet = ActiveSheet.Name
'capture meeting data already filled out
For j = 0 To UBound(datacolumns) - 1
For i = 1 To 3
If Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value <> "" Then
MeetingData(i - 1, j) = Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value
End If
Next i
Next j
'make hidden template visible
Worksheets("hiddenPrintLayoutTemplate").Visible = True
'Rename current Sheet
Sheets(currentSheet).Name = "used_Print_Layout"
''add a new sheet
' ActiveWorkbook.Worksheets.Add(before:=Sheets("used_Print_Layout")).Name = "PrintLayout Template"
'copy hiddentemplate before current sheet
Worksheets("hiddenPrintLayoutTemplate").Copy before:=Sheets("used_Print_Layout")
ActiveSheet.Name = currentSheet
'set rowheight for title rows
Range("A12").EntireRow.RowHeight = 24
Range("A18").EntireRow.RowHeight = 24
'delete current used printlayout
Worksheets("used_Print_Layout").Delete
'refilled meeting data
For j = 0 To UBound(datacolumns) - 1
For i = 1 To 3
If MeetingData(i - 1, j) <> "" Then
Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value = MeetingData(i - 1, j)
End If
Next i
Next j
'hide PrintLayout template
'Worksheets("hiddenPrintLayoutTemplate").Visible = xlSheetVeryHidden
'Sheets("PrintLayout Template").Select
'activate screenupdating and display warnings
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
When run it in macro mode on button, it runs, but excel crashes, when it is done. I cannot find what the issue is. Any ideas?
I am not sure if by debugging you mean stepping through line by line, but you could try inserting stop statements at key points in the code. So for example, you could put a stop statement in the following part:
'capture meeting data already filled out
For j = 0 To UBound(datacolumns) - 1
For i = 1 To 3
If Worksheets(currentSheet).Cells(i, Range(datacolumns(j)).Column).Value <> "" Then
MeetingData(i - 1, j) = Worksheets(currentSheet).Cells(i,Range(datacolumns(j)).Column).Value
End If
Next i
Next j
stop
'make hidden template visible
Worksheets("hiddenPrintLayoutTemplate").Visible = True
You could see if the code runs fine up until that point (i.e. run it without debugging). If it does, remove the stop statement and place it further down the code. Repeat this until you find the statements which cause your crash - perhaps the reason would appear then.
In general, if you get an odd crash in Excel VBA, try switching the Windows default printer to Microsoft XPS Document Writer. Seems odd, but that has worked for me on problems where I've wasted many hours only to find this is the culprit.