I am trying to write extended meta data of the file "Tags" that I read to excel with
ws.Cells(1, 1).Value = objFolder.GetDetailsOf(objFolderItem, 18)
But now I am looking for the oposite function to write it to the file from Excel
Related
I'm trying to save an autofilter, to then do some things on my worksheets and re-apply the filter.
Following this topic : In Excel VBA, how do I save / restore a user-defined filter? ; it was about to work.
Then I tried to filter on a date column, and it doesn't works -->
It gives me the following error :
I've looking for hours on internet to found a solution, like the following post: Excel VBA Saving autofilter settings with dates; but I can't fix this problem...
I was also thinking about convert the entire column in number format, but it doesn't works as the hide cells are not converted and the filter doesn't convert the value in the filter list.
It seems like dates are not valid values to give for a Criteria1.. could it be?
Here is my code:
With myCurrentWorkbook.Worksheets(my_Of_To_Produce_Sheet).AutoFilter
With .Filters
ReDim filterArray(1 To .Count, 1 To 3)
For f = 1 To .Count
With .Item(f)
If .On Then
filterArray(f, 1) = .Criteria1 'it's on this line that it doesn't work
'I tried this solution found on internet
'filterArray(f, 1) = Replace(.Criteria1, Application.International(xlDecimalSeparator), ".")
If .Operator Then
filterArray(f, 2) = .Operator
If .Operator = xlAnd Or .Operator = xlOr Then
filterArray(f, 3) = .Criteria2
End If
End If
End If
End With
Next f
End With
End With
I have a program utilizing openpyxl that opens two existing Excel files. One is a simple worksheet with data that needs to be copied into the other file, which is a workbook containing many worksheets and with VBA macros that use this copied data. When the VBA code runs, though, I get a Runtime Error 6 overflow. The strange thing is that if I manually copy all of the required cells into the workbook, the macros run without a hitch. But when this process is automated, even though the values in the cells are identical, this error pops up. Below is the code which transcribes the data from one worksheet to the other.
def transcribe_client_data_to_workbooks():
_active_sheet_index = 5
for client in set(cdict.values()):
report_path = r'C:\Program Files\Notepad++\reports' + '\\' +
string.replace(client,'/','-') + '_report.csv'
wb = openpyxl.load_workbook('Bucket-Asset Allocation Model.xlsm',
read_only = False, keep_vba = True)
ws = wb.active
with open(report_path, 'rU') as f:
reader = csv.reader(f)
for row_index, row in enumerate(reader):
for column_index, cell in enumerate(row):
column_letter = get_column_letter((column_index+1))
ws[column_letter+str(row_index+1)] = cell
f.close()
wb.save('C:\\Program Files\\Notepad++\\workbooks\\' +
string.replace(client,'/','-') + '_workbook.xlsm')
I don't understand why I would receive this error message when this data is automatically copied in, but not when I copy/paste manually. I open the same workbook template each iteration and then save it as a new one after the appropriate data has been copied over. For reference, here is the section of VBA code where the error occurs.
Do Until Sheets("Raw Data").Cells(crow, 1).Value = Empty
aNumber = Sheets("Raw Data").Cells(crow, 1).Value
ticker = Sheets("Raw Data").Cells(crow, 9).Value
security = Sheets("Raw Data").Cells(crow, 8).Value
mValue = Sheets("Raw Data").Cells(crow, 12).Value
bAmt = Sheets("Raw Data").Cells(crow, 18).Value
uGain = Sheets("Raw Data").Cells(crow, 20).Value
Do Until Sheets("Accounts").Cells(acrow, 1).Value = aNumber
acrow = acrow + 1
Loop
Specifically it occurs on the line that increments acrow:
acrow = acrow + 1
I tried changing acrow from an integer to a long, but that just causes the program to run indefinitely until I click or cancel it, at which point I receive Run-time error '1004', Application defined or object-defined error, on the line directly preceding the increment of acrow.
Any help would be appreciated, thanks!
John Coleman was correct about the location of the overflow. The issue was that I was writing aNumber as strings and aNumber was declared in the VBA as Variant type, which according to the docs can represent anything other than a fixed length string. I simply changed my strategy and wrote account numbers as integers which resolved the problem.
Sheet 1 in workbook A contains some cells with formulas that are protected and some cells that are used to enter data that are unprotected. There are multiple files that uses this structure.
I have now updated the formulas in a template file that use the same structure.
What I want to do is to copy all unprotected cells and move them to the same place in the new file.
Let’s say the range is A1:R99 and I have unprotected cells in B1:B99 and G1:G99 (multiple different places in real file so entering all different ranges would take a long time) Then I want to copy B1:B99 to the same place in the new file.
Hope there is a better way to do this than to enter every range manually!
Suppose you have the data in 'Sheet1' and if you want to copy all the unprotected cells to a new sheet, lets say 'Sheet2' in the same location as they were in 'Sheet1', then
Sub Foo()
If Sheets("Sheet1").ProtectContents = True Then
Worksheets("Sheet1").Activate
For i = 1 To 99
For j = 1 To 18
If Cells(i, j).Locked = False Then
a = Cells(i, j).Value
Worksheets("Sheet2").Activate
Cells(i, j).Value = a
Worksheets("Sheet1").Activate
End If
Next j
Next i
End If
End Sub
New to vba and trying to write a simple if/then statement in sequential order to return a value from different sheets.
If Cells(4, 4).Value = "water stress" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws2.Range("a3:b12"), 2)
End If
If Cells(4, 4).Value = "fire" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws3.Range("a3:b12"), 2)
End If
When i run the code with only the first if statement, it runs fine. When I add in the second if statement, I get a run-time error '1004' and am unable to get the vlookup property of the worksheetfunction class. I am new, and not sure how to trouble shoot.
I am trying to copy data from one Excel workbook to another.
We had a change in the template of an import file, and it has thus ruined the old import files. So there are some files that still need to be imported, but they are under the old template.
My issue stems from when I try to copy the data (paste special, values, anything tried) it gives me an error sometimes: "The Cell or chart that you are trying to change is protected and therefore read-only".
However, that isn't exactly the case. I've determined that it gives that error when I paste a blank cell onto a new field that has a drop-down with Yes or No. Yet, if I manually go to that cell and give it something blank (hit backspace + enter), it has no problems.
I've tried coding so it copy/paste's each line at a time from workbook to workbook, but my problem still remains for these cells that require a drop-down answer. I'm thinking that these cells need to be coded to actually be "typed" instead of pasted. It can't be a part of pasting the actual range.
Does anyone have an idea of how best to resolve this? Below is my current code, it is copying based on the range(s). It's very sloppy as the only way I can think is to keep switching from workbook to workbook. Any help is greatly appreciated.
Also, I'm not 100% on how to calculate the LastRow? So I just have it entered manually.
Sub MoveText()
For Row = 5 To 962
Workbooks("Data.xls").Activate
ActiveSheet.Range(Cells(Row, 1), Cells(Row, 3)).Select
Selection.Copy
Workbooks("blankTemplate.xls").Activate
ActiveSheet.Range(Cells((Row + 1), 1), Cells((Row + 1), 3)).Select
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks("data.xls").Activate
ActiveSheet.Range(Cells(Row, 5), Cells(Row, 29)).Select 'this will select the contents of the active row
Workbooks("blankTemplate.xls").Activate
ActiveSheet.Range(Cells((Row + 1), 5), Cells((Row + 1), 29)).Select
Selection.PasteSpecial Paste:=xlPasteValues
Next
Sub MoveText()
Dim shtData As Worksheet, shtTempl As Worksheet
Dim Row As Long
Set shtData = Workbooks("Data.xls").Sheets("Data") 'or e.g. .Sheets(1)
Set shtTempl = Workbooks("blankTemplate.xls").Sheets("Data")
For Row = 5 To 962
shtTempl.Cells(Row + 1, 1).Resize(1, 3).Value = _
shtData.Cells(Row, 1).Resize(1, 3).Value
shtTempl.Cells(Row + 1, 5).Resize(1, 25).Value = _
shtData.Cells(Row, 5).Resize(1, 25).Value
Next Row
End Sub