excel-vba

Accessing Excel sheet without mentioning its name in VBA program

假装没事ソ 提交于 2020-01-06 23:47:19
问题 How can I refer Excel sheet using VBA which has only one sheet without mentioning the sheet name in my program? Actually the sheet name might change next time with new data and it is creating a problem in my automation process. 回答1: If your macro is running on the same file, you can simply refer to it using the global ActiveWorksheet object. If you are loading it from somewhere else, you will be probably doing something like this: Dim objWorkbook as Workbook Dim objWorksheet as Worksheet Set

macro that loops through worksheets

╄→尐↘猪︶ㄣ 提交于 2020-01-06 22:44:30
问题 The macro below performs a calculation and generates a bar chart. At the moment it works for the first worksheet (Sheet1) I would like to be able to repeat the same macro on all worksheets in my excel workbook. Is there a simple way to do this? Thanks in advance. Sub MyReport() Workbooks.Open Filename:= _ Application.GetOpenFilename Range("G2").Select ActiveCell.FormulaR1C1 = "=SUM(C[-5])" Range("H2").Select ActiveCell.FormulaR1C1 = "=SUM(C[-5])" Range("I2").Select ActiveCell.FormulaR1C1 = "

VBA causing typed information to go to an incorrect worksheet

南笙酒味 提交于 2020-01-06 21:22:09
问题 I am creating a spreadsheet that creates a reference number on the first worksheet (called database, to be used similarly to a database) and generates a new worksheet. This then gives a reference number on the new worksheet so that they are linked together. This is done by pressing "New Idea" on a UserForm. Once this is completed it should then go to the newly created worksheet and highlight cell C7. Once this is complete it should close the UserForm and allow the user to be able to type in

VBA causing typed information to go to an incorrect worksheet

蓝咒 提交于 2020-01-06 21:22:07
问题 I am creating a spreadsheet that creates a reference number on the first worksheet (called database, to be used similarly to a database) and generates a new worksheet. This then gives a reference number on the new worksheet so that they are linked together. This is done by pressing "New Idea" on a UserForm. Once this is completed it should then go to the newly created worksheet and highlight cell C7. Once this is complete it should close the UserForm and allow the user to be able to type in

Check if sheet exists

人走茶凉 提交于 2020-01-06 21:20:43
问题 Can someone please advise where I'm going wrong here? I want to check if the sheet named "test" exists and if not, create and name that sheet to "test". If it exists, I run a separate block of code which I haven't put up here. I have used error handling in that it ignores the errornif it happens. If Sheets("test").Name = "" Then 'MsgBox Sheets("test").Name & "Name" .Worksheets.Add After:=ThisWorkbook.Worksheets("test2") .ActiveSheet.Name = "test" End If No matter what I do, this section of

How do I get all the different unique combinations of 3 columns using VBA in Excel?

≡放荡痞女 提交于 2020-01-06 21:10:52
问题 I have an Excel worksheet with several columns, where 3 of them form a "unique key". If I have fruits in column A (Apple, Banana, Orange), some name in column B (John, Peter) and something like Yes/No in column C, I want to be able to get sums of values from rows where the values in these columns are the same. For instance, the sum of all the values in column D for rows where columns A, B and C are Apple,John,Yes. Sorry for the confusing text, but I don't know how to express my question more

Random '1004 application-defined or object-defined error'/'1004 PasteSpecial method of Range class failed'

时光总嘲笑我的痴心妄想 提交于 2020-01-06 20:57:51
问题 A macro (not mine though, I "inherited" it) runs multiple loops. The code has a couple of thousands lines so I will provide only the wonky part of the loop in the snippet. Dim repoLastRow As Integer, repoLastCol As Integer Worksheets("ATT_LEV").Activate With ActiveSheet repoLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With repoLastCol = ActiveSheet.Cells(3, Columns.Count).End(xlToLeft).Column Cells(repoLastRow + 1, 1).Value = xmlAgreement1 Cells(repoLastRow + 1, 2).Value = repoLastRow

How to set different bits in VBA

北慕城南 提交于 2020-01-06 20:18:12
问题 I need to use bit operations in VBA. For example I want to set first bit in 0 second bit in 1 and so on. How can I do it? Thanks a lot! 回答1: You could use masks. If you want to set* the n-th bit, you can perform an Or operation of your original value and a mask filled with 0 except for the n-th position. That way, 1100 Or 0001 will set the first bit resulting in 1101 . 1100 Or 0010 will set the second bit, resulting in 1110 . If you want to unset* the n-th position, you can do the opposite.

Excel 365 Print Preview

╄→尐↘猪︶ㄣ 提交于 2020-01-06 20:18:09
问题 I'm using Excel 365 on Windows 7 Home 64bit. When I open Print Preview from the Excel user interface I get a window with options like this: https://www.flickr.com/photos/132482128@N08/21711947524/in/dateposted-public/ From this window I can preview the whole print file, make adjustments to margins, select the printer to use, etc. I would like to access this window and these options from VBA. However when I use: ActiveWorkbook.Sheets("SheetName").PrintOut Preview:=True or: ActiveWorkbook

VBA: Error on creating a pivot table

耗尽温柔 提交于 2020-01-06 20:11:37
问题 I am creating a VBA macro. Among other things, it makes a pivot table: ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Working").UsedRange).CreatePivotTable TableDestination:="", TableName:="HDPivotTable", DefaultVersion:=xlPivotTableVersion10 This works, but when I delete all data from the Excel spreadsheet and replace it with new data of the same structure, I get an error on the above line: Run-time error '13': Type mismatch Could you please help me on this?