excel-vba

VBA: Can't change Image Control picture after click on element

。_饼干妹妹 提交于 2020-06-27 15:41:13
问题 Setup I have an Excel VBA UserForm with an Image Control element. When clicking on buttons in the form, the Image Control's Picture source is set / updated with the following code: Set MyForm.imgControl.Picture = LoadPicture(pathToFile) Problem When the user clicks on the Image Control, updating its Picture source doesn't work anymore. The problem occurs, no matter when in the workflow the Image Control is clicked: Example 1: User clicks a button to set the Picture (and pictures sets

How can I use the WEEKNUM function in a statement in VBA?

拈花ヽ惹草 提交于 2020-06-27 13:10:11
问题 I want to write a VBA, to check if WEEKNUM or ISOWEEKNUM equal to the value, then run the rest of macro. I've tried to do that but I got an error because of using TODAY as arg. 回答1: Here is one way to use both WEEKNUM() and TODAY() in VBA: Sub dural() If Evaluate("=weeknum(today())") = 28 Then MsgBox 28 Else MsgBox "not 28" End If End Sub 回答2: Use the WorksheetFunction object or Excel Application object to call native functions into VBA. debug.print WorksheetFunction.WeekNum(Date) debug.print

Excel VBA - .Find method between workbooks

北慕城南 提交于 2020-06-27 10:46:34
问题 Question: Why is the Range.Find method not working when referencing a different workbook? Problem: I'm attempting to copy data between workbooks, but the Range.Find method is stopping with a "Run-time Error 1004". I'm using Excel 2007 on a Windows 7 machine. Details: On two workbooks, only Sheet1 is referenced or used for each workbook. I have a procedure (ztest) with the following outline: Format the sheet Loop through all cells in column E of workbook #1 Using the Range.Find method, find

Excel VBA - .Find method between workbooks

纵然是瞬间 提交于 2020-06-27 10:46:09
问题 Question: Why is the Range.Find method not working when referencing a different workbook? Problem: I'm attempting to copy data between workbooks, but the Range.Find method is stopping with a "Run-time Error 1004". I'm using Excel 2007 on a Windows 7 machine. Details: On two workbooks, only Sheet1 is referenced or used for each workbook. I have a procedure (ztest) with the following outline: Format the sheet Loop through all cells in column E of workbook #1 Using the Range.Find method, find

Add a gradient to a shape/line

房东的猫 提交于 2020-06-27 10:04:14
问题 I am attempting to add a gradient to a line shape in Excel using VBA. This feature is available in the Line Color section under the Format Shape option. Despite this feature existing under the Format Shape option, I am unable to reproduce the functionality in VBA. My code is: With ActiveSheet.Shapes("Straight Connector 4") .Line.ForeColor.RGB = RGB(193, 193, 193) .Line.Transparency = 0.25 .Line.Visible = msoTrue .Line.ForeColor.SchemeColor = 24 .Line.BackColor.SchemeColor = 34 .Line

How to make Selection.AutoFilter starts in row 3 instead of row 1

妖精的绣舞 提交于 2020-06-27 09:10:15
问题 I have this code: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean) Application.ScreenUpdating = False If Target.Value <> "" Then Set wbks = Workbooks.Open("\\MyPath\Workbook.xlsx") wbks.Sheets("Control").Activate ActiveSheet.Range("A1").Select Selection.AutoFilter Selection.AutoFilter Field:=7, Criteria1:=Target.Value '7 is the filter # column End If End Sub It works well only if headers in the sheet control are located in row 1. My problem is that \

VBA: New collection -> A module is not a valid type

流过昼夜 提交于 2020-06-27 08:56:15
问题 I'm trying to use a collection as part of a function, however I keep getting the error: "A module is not a valid type" on compile. Even if the function simply defines a collection, I get the same: Function CountUniqueTags() Dim table As Collection Set table = New Collection End Function This code is in a standard module, but the error implies I should be writing this in a class module, but Collection is a built-in class so I don't see the issue? 回答1: This was making me go crazy for a while,

function to convert String to upper case

夙愿已清 提交于 2020-06-27 07:50:08
问题 I have been trying to make a user defined function I wrote return it's value in all upper case, using the String.ToUpper() method in VBA. When I try to use my UDF in excel, I get a compiler error that just highlights the top line of my UDF: Function removeSpecial(sInput As String) As String Here is the code in it's entirety: Function removeSpecial(sInput As String) As String Dim sSpecialChars As String Dim i As Long sSpecialChars = "\/:*?™""®<>|.&@# (_+`©~);-+=^$!,'" 'This is your list of

VBA: Copy As Image from Excel and Paste into Word

社会主义新天地 提交于 2020-06-27 06:12:11
问题 I'm copying a range of cells from Excel as a picture and then pasting into a word document. It pastes at the beginning of the document, how could I set it to paste in a specific area? The area could be denoted by some text that I'd later find/replace. Thanks! Range("A1:H5").Select Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture Set objWord = CreateObject("Word.Application") Set objDoc = objWord.Documents.Open("MyFile.docx") objWord.Visible = True Set objSelection = objWord

How to highlight rows with different colors by groups of duplicates?

谁说胖子不能爱 提交于 2020-06-27 04:14:06
问题 How do I highlight rows with different colors by groups of duplicates? I don't care about which colors are used per se, I just want the duplicate rows one color, and the next set of duplicates another color. For example, if I wanted the '1s' green, the '2s' blue and so on. It goes up to 120 in my column. Thank you. 回答1: Try out this simple code and modify it per your needs. Its quite self explanatory, Sub dupColors() Dim i As Long, cIndex As Long cIndex = 3 Cells(1, 1).Interior.ColorIndex =