Macro

APML

Board Regular
Joined
Sep 10, 2021
Messages
216
Office Version
  1. 365
Hi All.. I'm not very good at macros or vba, pls keep this in mind while reading my issue.

In the view code section of a tab in my sheet, I have the code as per the screenshot below. These codes operate via a button on the sheet. I want to get rid of the button and just use a link to trigger a macro that does same thing. (which I know how to do).
My issue is that I don't know how to do this. i.e (i want the following to be a named macro).. Hope this question makes sense :)
Private Sub CommandButton2_Click()
Workbooks.Open "C:\CSVToday\1a.csv"
Workbooks("1a.csv").Sheets("1a").Range("A4:G2000").Copy
ThisWorkbook.Sheets("TodaysData").Range("C4").PasteSpecial Paste:=xlPasteValues
ActiveWindow.WindowState = xlMinimized
MsgBox ("Data Has Been Updated")


1661206273594.png
 

Attachments

  • 1661205931412.png
    1661205931412.png
    56 KB · Views: 1

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Then move your button click event code into such a macro?
As mentioned I'm not so great at this, so pls excuse me if this sounds like a silly question. Where do I find the button click events?
 
Upvote 0
You posted one.
Private Sub CommandButton2_Click()
So you'd move this code to the link code you say you know how to create?
VBA Code:
Workbooks.Open "C:\CSVToday\1a.csv"
Workbooks("1a.csv").Sheets("1a").Range("A4:G2000").Copy
ThisWorkbook.Sheets("TodaysData").Range("C4").PasteSpecial Paste:=xlPasteValues
ActiveWindow.WindowState = xlMinimized
I'm not trying to be a smarta$$ - I don't actually know what you mean by using a link to run code so you got me there.
 
Upvote 0
You posted one.
Private Sub CommandButton2_Click()
So you'd move this code to the link code you say you know how to create?
VBA Code:
Workbooks.Open "C:\CSVToday\1a.csv"
Workbooks("1a.csv").Sheets("1a").Range("A4:G2000").Copy
ThisWorkbook.Sheets("TodaysData").Range("C4").PasteSpecial Paste:=xlPasteValues
ActiveWindow.WindowState = xlMinimized
I'm not trying to be a smarta$$ - I don't actually know what you mean by using a link to run code so you got me there.
Ah sorry, Im probably using the wrong terminology. Im meaning to say that I'll create a hyperlink in a cell (lets say cell L1), when i hit the hyperlink it will trigger the named macro. I prefer this because I find the buttons awkward to use. Here's an example of what I mean:

End If
If Target.Range.Address = "$L$1" Then
Call ClearFilters
 
Upvote 0
Ah sorry, Im probably using the wrong terminology. Im meaning to say that I'll create a hyperlink in a cell (lets say cell L1), when i hit the hyperlink it will trigger the named macro. I prefer this because I find the buttons awkward to use. Here's an example of what I mean:

End If
If Target.Range.Address = "$L$1" Then
Call ClearFilters
Sorry just read the previous post prior to the last, I'm going to try that now, will let you know how I go. BTW, I really appreciate your help
 
Upvote 0
Yes, I've got it. 1 last question, how do I write the following as code (so I can use it in a macro like above)?

Do While fName <> "" & ""

If fName <> "1a.csv" Then 'or .txt or .csv or whatever

Kill "C:\CSVToday\" & fName



End If

fName = Dir

Loop

MsgBox ("All Files Hvae Been Deleted")


End Sub
 
Upvote 0
Don't know what event you're using for this hyperlink selection. If it's the WorksheetSelection_Change event you don't really need the hyperlink?
Looks to me that what you posted should work, although I don't see why not Do While fName <> "" instead of Do While fName <> "" & ""
I'd probably write it this way though;
VBA Code:
Do While fName <> ""
   If fName <> "1a.csv" Then Kill "C:\CSVToday\" & fName
   fName = Dir()
Loop
You should use code tags to maintain proper indentation and make code easier to read (vba button on posting toolbar).
 
Upvote 0
Solution

Forum statistics

Threads
1,214,566
Messages
6,120,262
Members
448,953
Latest member
Dutchie_1

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