Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have list of Excel file paths in a text file (output from a VBA macro).
How can I use vba to open these files?
Thanks
Something like this which will skip over invalid filenames/paths
It opens any valid workbooks stored in a textfile, C:\temp\test.txt, then closes them in turn
Sub Alistair_Cooked()
Dim objFSO As Object
Dim objWB As Workbook
Dim strFN As String
Dim objTF As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile("C:\temp\test.txt")
On Error Resume Next
Do While Not objTF.AtEndOfStream
strFN = objTF.readline()
Set wb = Workbooks.Open(strFN)
If wb Is Nothing Then
Debug.Print strFN
Else
'do something
wb.Close False
Set wb = Nothing
End If
Loop
On Error GoTo 0
End Sub
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Please help debug: Run-time error '438' Object doesn't support this property or method
For this code to copy value from workbook to another, i do that yesterday but i forgot saving that, i try today but it doesn't work
Sub Listcustomer()
Dim y As Workbook
Dim x As Workbook
Set y = ActiveWorkbook
Set x = Workbooks.Open("\\myNetworkSharePath\myFileName.xlsx")
For i = 1 To x.Sheets.Count
y.ActiveSheet.Range("d" & i + 3).Value = x.Sheet(i).Range("b3").Value
Next i
End Sub
Typo. Change Sheet to Sheets
y.ActiveSheet.Range("d" & i + 3).Value = x.Sheets(i).Range("b3").Value
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to apply a VBA to a workbook with multiple worksheets where sheetnames are variable but fail.
MY code only works on active worksheet.
Could please help me to see what is the problem?
Sub sample_code()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
Range("A1").Value = "test each worksheet"
End With
Next ws
End Sub
Sorry, can't test this right now, but does the following work?
Sub sample_code()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.Range("A1") = "test each worksheet"
' (.Range("A1").Value is okay too)
End With
Next ws
End Sub
You should fully qualify your Cell A1 range - example .Range("A1")
Intro to VBA: The Excel Object Hierarchy ( Jon Acampora )
Look into VBA objects which are organized in a hierarchy so it makes them easier to reference an object
At the top it is the Excel Application. All the objects within Excel are members or sub-members of the Application object.
The dots between each word allow us to reference members of the hierarchy from top down
Remember VBA allows us to make assumptions when referencing objects. If you don’t specify the workbook or worksheet in a line of code, then VBA assumes you are referring to the active workbook and active worksheet.
For example, the following line of code will clear the values and formulas of all the cells on the active worksheet.
Cells.ClearContents
If you don’t tell VBA which sheet in which workbook you want to clear, then this could spell disaster! You can’t undo that action.
So you would want to qualify this line of code to tell VBA which workbook and worksheet you are referring to.
Workbooks("Book1.xlsx").Worksheets("Sheet1").Cells.ClearContents
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have around 500 files of excel and the most part have filter. Now i need to put all information in one sheet. I found how to put all in one sheet but the books with filter only copy information with filter and i need all.
So, i need to remove filter of all excel files. Important (not all files have filter)
Thanks for your help ;)
Before you copy the data, you should use:
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
This will turn off the filter.
Will remove any filter on all opened workbooks, have fun :)
sub try_this()
dim wb as workbook
dim sh as worksheet
for each wb in workbooks
for each sh in wb.worksheets
sh.AutoFilterMode = False
next
next
end sub
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hi my problem is when the code searches (i, 1) to find "Hello" it finds it but won't send it to WSS and the more I look the frustrated I get can someone help please
Sub AddSelection()
Dim WSD As Worksheet ' Sheet 1 as prices sheet
Dim WSW As Worksheet ' Workings sheet as Information
Dim WSS As Worksheet ' Selections worksheet
Set WSD = Worksheets("Selection")
Set WSW = Worksheets("Workings")
Set WSS = Worksheets("Selections")
' Loop through records on WSD column A
FinalRow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
For i = 5 To FinalRow
If WSD.Cells(i, 1) = "Hello" Then
' When I run the code this where the problem is
WSD.Cells(i, 1).Copy Destination:=WSS.Cells(NextRow, 4)
NextRow = NextRow + 2
FinalRow = WSS.Cells(Rows.Count, 1).End(xlUp).Row
End If
Next i
'Make sure WSR is the active sheet
WSS.Select
' Report that the macro is done
MsgBox prompt:=NextRow - 1 & " Results Records Are Copied To Worksheet."
End Sub
NextRow is never defined. You need to define it before using it.
To stop making this mistake, write this at the top of every "file" in your VBA project:
Option Explicit
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to learn how to copy cells from an already open excel workbook to another file. I don't just want a code, I want a tutorial. The problem I have is Google is only giving me forums. Can someone suggest a good tutorial with a video? A hyper link would help.
Option Explicit
Sub CopyFromOpenWorkbookToAnother()
'declare variables
Dim wkbCopy as Workbook, wkbPaste as Workbook
Dim wksCopy as Worksheet, wksPaste as Worksheet
Dim rngCopy as Range
'set variables -> change the names to suit your data needs
Set wkbCopy = Workbooks("WorkbookToCopyFrom.xls")
Set wkbPaste = Workbooks("WorkbookToCopyTo.xls")
Set wksCopy = wkbCopy.Sheets("SheetToCopy")
Set wksPaste = wkbPaste.Sheets("SheetToPaste")
Set rngCopy = wksCopy.Range("A1:A10") 'adjust to whatever range you need
rngCopy.Copy wksPaste.Range("A1") 'adjust to wherever you want to paste
End Sub