Tag: vba

用电子邮件将电子邮件中的Excel工作表的一部分作为PDF附件用button发送

我在上面的标题中有代码,下面的代码将把电子邮件中的Excel表单的PDF发送到单元格中指定的电子邮件地址。 我的问题是它给PDF文件的名称是文件名后跟标签名称。 我将如何编辑下面的代码,以便为文件提供表单中单元格中指定的名称? Sub savePDFandEmailPayPlan() Dim strPath As String, strFName As String Dim OutApp As Object, OutMail As Object 'Create PDF of active sheet only strPath = Environ$("temp") & "\" trailing "\" strFName = ActiveWorkbook.Name strFName = Left(strFName, InStrRev(strFName, ".") – 1) & "_" & ActiveSheet.Name & ".pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ strPath & strFName, […]

将所有非空白行快速移动到ws顶部

我试图将所有非空白行移动到我的工作表顶部(实际上是一个自动Autofilter的顶部)。 我已经意识到,简单地删除空行是非常缓慢的,而且更快的替代方法是将范围分配给变体。 我已经拿出这个代码,但是由于某种原因,它正在失去一些行: Public Sub CompactRows(ByRef ws As Worksheet) Dim a As Variant With ws a = .AutoFilter.Range.Offset(1, 0).Columns(1).SpecialCells(xlCellTypeConstants).EntireRow .AutoFilter.Range.Offset(1, 0).Clear .AutoFilter.Range.Cells(2, 1).Resize(UBound(a), .UsedRange.Columns.Count) = a End With End Sub 所以,如果我有1000行,以空行分隔,创build100个子范围,function结束后,我只有100行(其他900丢失)。 我注意到在这种情况下Ubound(a)也返回100.我的理论是它只复制每个子范围中的第一行,但我不确定。 任何解决这个问题,或另一个更快的替代scheme,以达到同样的结果,将不胜感激。

数组值不转置给单元格

我正试图学习如何在Excel VBA中使用字典。 testing是通过一个数组将字段A中1-100000行的所有值传递给一个字典,然后将所有值写入列B.这个工作正常,直到行34464,B列中的其余行得到#N/A 任何想法为什么? Sub nnn() 'Tools – References – Microsoft Scripting Runtime Dim myArray As Variant Dim myRow As Long Dim dicMyDictionary As Scripting.Dictionary Set dicMyDictionary = New Scripting.Dictionary With ThisWorkbook.Worksheets("Sheet1") myArray = Range(Cells(1, 1), Cells(100000, 1)).Value For myRow = LBound(myArray, 1) To UBound(myArray, 1) dicMyDictionary.Add myRow, myArray(myRow, 1) Next myRow myArray = dicMyDictionary.Items […]

剪切并复制特定的string值到相邻的单元格

我在同一个单元格中有一个公司名称和城市名称的Excel数据库,我想将城市名称拆分到find文本的相邻单元格。 我有一个数组的城市名称。 数据库看起来像: FINER附近EL CAJON 第一个selectpipe道系统RAMONA FRANK GILILLAND施工SANTA YNEZ 等等 我的代码到目前为止(非常基本): Sub FindCityNames() Dim citylist(500) As String Dim i, j, k As Integer For j = 1 To 500 citylist(j) = Workbooks("cities.xlsm").Worksheets("City list").Cells(j, 1) Next j For i = 1 To 500 Cells.Find(What:=citylist(i), After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate ActiveRange.Cut Next i […]

使用工作表1中的值,search工作表2,并将工作表中的值作为工作表3返回

这个问题我在第2页有一堆数据。大约有6k行。 我有一些437我想find。 这些在表1(A栏)中有规定。 对于这些我想复制整个行,并将其放置在工作表3中。工作表1中的值可以在工作表2中多次find,我需要他们。 我的解决scheme,我发现VBAsearch这一切。 但是在437点停止。 Public Sub findfak() Dim lastRowS1 As Long Dim lastRowS2 As Long Dim lastRowS5 As Long Dim i As Long Dim j As Long Dim tempS1 As Long Dim temps2 As Long Dim tempRow As Long lastRowS1 = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row lastRowS2 = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row Application.ScreenUpdating = False For i = […]

将值分配给2维数组

我想获得一些数据,我input另一个macros到一个二维数组,所以我可以应用一个函数的数据,但不pipe我尝试我不断收到错误。 数据包括string和数字。 我总是可以引用单元格,忘记数组,但这会使函数复杂化。 这是我的代码: (声明) Dim nLiens As Byte, nCreditors As Byte Dim SecurityV As Currency, ASecurityV As Currency Const adjuster = 0.9 (相关潜艇) Public Sub VariableDeclaration() nLiens = InputBox("Enter number of liens in security") nCreditors = InputBox("Enter number of creditors") SecurityV = InputBox("Enter security full value") ASecurityV = adjuster * SecurityV Call ODebt End […]

Excel VBA:无法定义范围,获取块variables未设置的错误对象

我想在Excel中使用VBA定义一个Range, Sub convertePerc() Dim separador As String Dim linhaInicial, linhaFinal, colunaInicial, colunaFinal, numAnos As Integer Dim origem, destino As Range separador = "DRES(G)" colunaFinal = Sheets(separador).Cells(6, 5).End(xlToRight).Column linhaFinal = 40 numAnos = 10 origem = Sheets(separador).Range(Cells(10, 4), Cells(linhaFinal, colunaFinal)) colunaInicial = CInt(4 + numAnos + 1) colunaFinal = CInt(numAnos + colunaFinal + 1) destino = […]

Excel VBA脚本格式dateMM-DD-YYYY

我需要将两个单元格的值从dd / mm / yyyy转换为mm-dd-yyyy,并将这些值附加到存储过程调用中。 我的代码: Dim dtInicio As String Dim dtFim As String Set stDate = CStr(MWS.Cells(1, "E").Value) Set enDate = CStr(MWS.Cells(2, "E").Value) 上面的代码后,我需要这样的东西: wstring = "exec sp_accessFile '"+stDate+"', '"+enDate +"'

debugging模式下的公式与不同的值不同

所以我正在写一个excel VBA程序,它将具有特定名称的所有csv文件读入工作簿上的不同表格中。 它正在阅读没有问题的文件。 问题出现在将数据从文件转换到数组中,这意味着要包含目录中所有文件中的所有相关数据。 要读取文件并按行分隔, 我在用: Do Until EOF(1) Line Input #1, LineFromFile lineitems = Split(LineFromFile, Chr(10)) 在模块的下面,当符合该行数据中的特定条件时,我将这些lineitems传递给数据数组。 为了清楚起见,expression式将文件行中的date值与文件名称上的date标记进行比较。 ElseIf (Left(lineitems(Line), 8) = Right(Left(FileTime(FileNum), 6), 2) + "/" + Right(Left(FileTime(FileNum), 4), 2) + "/" + Left(FileTime(FileNum), 2)) Then Proxy = CStr(lineitems(Line)) DataArray(InverterNumber – 1, m(InverterNumber – 1)) = Proxy 正如你所看到的,我已经添加了一个代理步骤来尝试并确保数据通过,但无济于事。 我还添加了一个故障保险,以确保没有空行进入DataArray: If Len(DataArray(InverterNumber – 1, […]

1秒延迟持续近2秒

我正在编写一个代码,将数据从一个电子表格复制到另一个电子表格。 我已经尝试了Application.Wait和睡眠,但他们阻止两个电子表格,所以我决定使用一个做直到循环。 它有效,但1秒持续近2秒,我不知道为什么。 所以我只留下了代码中的循环,但testing给出了相同的结果(大约需要95秒)。 任何build议? 这里是代码: Sub Test() Dim Delay As Date cell = 1 For i = 1 to 60 Workbooks("Data").Worksheets("Sheet1").Range("C" & cell).Value = cell cell = cell +1 Delay = Now() + TimeValue("00:00:01") Do Until Now() >= Delay Do Events Loop Next i End Sub