Macro Button

Jbear2386

New Member
Joined
Jul 24, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have created a custom button in the ribbon. I would like to have an Update Macro assigned to it, but having trouble with the coding. This is for my client I'm just trying to automate he's experience. Essentially I want him to be able to click a button daily that opens file and he is able to import another file to append to the master file I'm building him with no additional headers. So I did Get data and was able to append through there; however I don't want him to have to go through all that. As well as I'm not sure where the files will be stored so I really just want him to have the option to select file then it auto appends. Then after have a secondary button that hides all unnecessary rows, and a third to reset the rows for the next import.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Long story short, all elements have so called called callbacks. They must have strictly defined signature. For the button it will be:
VBA Code:
Sub OnButtonClick(ctrl As IRibbonControl)
  '// do your thing
End Sub
Your XML must look like this to connect your button and procedure:
XML:
<button id="rxID" onAction="OnButtonClick" size="large" imageMso="ShowNotesPage"/>
I would suggest to use specialized software for automatically generating required stuff. I would suggest RibbonXMLEditor. This is cool program I use till today. As for callbacks, this page shows all callbacks for all elements.
 
Upvote 0
This is the code I had previously but it is not working

Sub Update(control As IRibbonControl)

On Error GoTo here:


Application.ScreenUpdating = False

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Dim wb2 As Workbook
Dim ws2 As Worksheet


Dim fileDialog As Office.fileDialog
Dim filePaths As Variant



' Create a file dialog object
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)

' Allow multiple file selection
fileDialog.AllowMultiSelect = True

' Add filter for Excel files
fileDialog.Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm", 1

' Show the file picker dialog

Dim lrow1, lrow2, i, j As Long
Dim hasMatch As Boolean

hasMatch = False

Dim lcol As Long
Dim tmp As Variant



If fileDialog.Show = -1 Then
For Each filepath In fileDialog.SelectedItems
' Here you can perform actions on each selected file
'Debug.Print "Opening file: " & filepath

Set wb2 = Workbooks.Open(filepath)

Set ws2 = wb2.Sheets(1)

lrow2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
lrow1 = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

Dim nameCol, mapCol, notesCol, dateCol, qtyCol, addressCol As Long

nameCol = Application.Match("Name", ws2.Rows(1), 0)
mapCol = Application.Match("Map", ws2.Rows(1), 0)
notesCol = Application.Match("Notes", ws2.Rows(1), 0)
dateCol = Application.Match("Date", ws2.Rows(1), 0)
qtyCol = Application.Match("Driver Note", ws2.Rows(1), 0)
addressCol = Application.Match("Address", ws2.Rows(1), 0)

If IsError(nameCol) Then
tmp = appendNameCol("Name", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: nameCol = tmp

End If
End If

If IsError(mapCol) Then
tmp = appendNameCol("Map", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: mapCol = tmp

End If
End If

If IsError(notesCol) Then
tmp = appendNameCol("Notes", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: notesCol = tmp

End If
End If

If IsError(dateCol) Then
tmp = appendNameCol("Date", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: dateCol = tmp

End If
End If

If IsError(qtyCol) Then
tmp = appendNameCol("Driver Note", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: qtyCol = tmp

End If
End If

If IsError(addressCol) Then
tmp = appendNameCol("Address", CStr(filepath), ws2)

If tmp = -1 Then
MsgBox "User cancelled."
wb2.Close
Exit Sub
Else: addressCol = tmp

End If
End If



For i = 2 To lrow2

If ws2.Cells(i, nameCol) <> "" Then

For j = 2 To lrow1

If ws2.Cells(i, nameCol) & "-" & ws2.Cells(i, mapCol) = ws.Cells(j, "A") & "-" & ws.Cells(j, "C") Then

hasMatch = True

lcol = ws.Cells(j, ws.Columns.Count).End(xlToLeft).Column


ws.Cells(j, 4) = ws2.Cells(i, notesCol)
ws.Cells(j, 4).WrapText = False

ws.Cells(j, lcol + 1) = ws2.Cells(i, dateCol)


ws.Cells(j, lcol + 1).NumberFormat = "m/d/yyyy"
ws.Cells(j, lcol + 1).HorizontalAlignment = xlCenter
ws.Cells(1, lcol + 1) = "DELIV DATE"

If ws2.Cells(i, qtyCol) <> "" Then
ws.Cells(j, lcol + 2) = CInt(ws2.Cells(i, qtyCol))
Else: ws.Cells(j, lcol + 2) = "-"
End If
ws.Cells(j, lcol + 2).HorizontalAlignment = xlCenter
ws.Cells(j, lcol + 2).WrapText = False
ws.Cells(j, lcol + 2).NumberFormat = "General"
ws.Cells(1, lcol + 2) = "QUANTITY"

Exit For

End If

Next j


If hasMatch = False Then

lrow1 = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ws.Cells(lrow1 + 1, "A") = ws2.Cells(i, nameCol)
ws.Cells(lrow1 + 1, "B") = ws2.Cells(i, addressCol)
ws.Cells(lrow1 + 1, "C") = ws2.Cells(i, mapCol)
ws.Cells(lrow1 + 1, "D") = ws2.Cells(i, notesCol)
ws.Cells(lrow1 + 1, "E") = ws2.Cells(i, dateCol)

ws.Cells(lrow1 + 1, "E").NumberFormat = "m/d/yyyy"
ws.Cells(lrow1 + 1, "E").HorizontalAlignment = xlCenter

If ws2.Cells(i, qtyCol) <> "" Then
ws.Cells(lrow1 + 1, "F") = CInt(ws2.Cells(i, qtyCol))
ws.Cells(lrow1 + 1, "F").NumberFormat = "General"

Else: ws.Cells(lrow1 + 1, "F") = "-"
End If
ws.Cells(lrow1 + 1, "F").HorizontalAlignment = xlCenter
ws.Cells(lrow1 + 1, "F").WrapText = False


End If
End If
hasMatch = False

Next i

ws.Rows(1).Font.Bold = True
wb2.Close



' For demonstration purpose, let's just print the file paths
Next filepath
Else
Debug.Print "No files selected."
End If

' Release the file dialog object
Set fileDialog = Nothing

MsgBox "Done Processing data."

Exit Sub
here:

MsgBox "Incorrectly done. Please make sure to close input files before using this macro."

End Sub


Function appendNameCol(str As String, pat As String, ws As Worksheet)

Dim updatedStr As String
Dim colName As Variant

Dim isOK As Boolean
isOK = False
updatedStr = InputBox(str & " column name cannot be found on " & pat & vbNewLine & " Please enter correct column name.", "Append column name")

Do Until isOK = True Or updatedStr = ""

colName = Application.Match(updatedStr, ws.Rows(1), 0)

If IsError(colName) = False Then
isOK = True

Else:
updatedStr = InputBox(str & " column name still cannot be found on " & pat & vbNewLine & " Please enter correct column name.", "Append column name.")
End If

Loop


If updatedStr = "" Or updatedStr = vbNullString Then
appendNameCol = -1

Else:
appendNameCol = colName

End If

End Function
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top