How to perform refresh all in excel through vba code - vba

I want to import records from a oracle database to an excel sheet by using the options "From Other sources" in the "Data" Tab. I need a vba code to open the excel file from a specific location and then press "Refresh All" Button.

To refresh the data in a workbook, you can use:
ActiveWorkbook.RefreshAll
Why do you need to open the excel file from a specific location? If the connection is stored in the workbook, you shouldn't need to.

Related

How to use macros for hyperlink in Excel

I have sharpoint site where I have uploaded the Excel.
I have few list each list have same excel hyper link..
What I want to perform..
In Excel I have some data, Onclick of first hyperlink I want to open same excel sheet but I want some row to be selected.
same I want when I click on other option it should open same excel but row should be different one slected.
I tried to acheive this using the Macros but not able kno, How i will configure for hperlink..
https://support.office.com/en-us/article/Quick-start-Create-a-macro-455512ef-3532-404e-b8dd-ea6589512c1b

Importing data into a master worksheet

I have a doubt about my program to the VBA/EXCEL
I made a file called "MASTER WORKSHEET" with various data from a client, my goal is: To create a VBA to open any file in Excel and that I import chosen cells to fill out all data of the "MASTER WORKSHEET", for example, all excel file I open with the button "IMPORT DATA", it will automatically capture only those cells that I choose, but my program it captures but is with configuration problems on VALUE or NUMBER.
If your question is simply "how do I copy a cell from one place to another", an example would be:
Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Copy(Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4"))
If you just want to copy the value, without the formula and formatting, you could use
Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4").Value = Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Value
Obviously, the workbook, sheet, and range names need to be changed to your particular situation.

Error on protected sheet

I have macro enabled workbook. Both Workbook and sheets are password protected.
Basically, I have 2 sheets. First "crater" which has a drop down list and buttons.
Second, "Uno" which has buttons again( Its a hidden sheet)
Button 1 on crater page task is to just copy the "Uno" sheet and create a new sheet. All tasks are working fine.
But my problem is, when I create a child sheet and save and close the workbook.
Next time, As soon as I open workbook I get an "error '1004'. You cannot use the command on protected sheet. You must first unprotect sheet" .
I would like to know, Where can I use the code to unprotect sheet in this case?
I am confused, please help
Use the command Worksheet("Your_Sheet_Name").Unprotect if you have a password you need to add it as well Worksheet("Your_Sheet_Name").Unprotect(Password)
You can have it in the Workbook Open and it will unlock the worksheet. However, then all the users can modify this worksheet as well.

Excel VBA - Paste from clipboard

I'm trying to build a macro that looks like this:
Sub CopyAsValues()
Selection.PasteSpecial xlPasteValues
End Sub
It seems to me that Excel requires from me to add a copy line, e.g.:
Range("C1:C5").Copy"
But I don't want to. I want to manually copy from cells, and then use the macro to paste as values. How can I do that?
You do not need macro for this, comfortable way to paste only values, I guess, will be using quick access toolbar as in picture below
Excel Option ~~> Customize ~~> Choose commands from [All Commands] ~~> Select "Paste Values" command and add to toolbar
so you will receive button in top of the excel window with required option
then you can copy and paste only values using this button

Sheet.Activate changes sheet but continues to edit data on previous sheet

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.
When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]
Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.
Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")
I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless