excel-vba

ODBC Connection Command Text

五迷三道 提交于 2020-01-07 04:27:09
问题 I use the below statement successfully many times in my VBA code. With ActiveWorkbook.Connections("Query").ODBCConnection .CommandText = SqlStat .Refresh End With I'm populating SQLStat based on parameters that are entered into Excel and inserted into the rest of the query. One of these parameters is from a column range within Excel. I have the below code to loop through the range, and get every value within this to put into one string, separated by commas. I then use this string within an IN

Check If Item Exists in Collection with Application.Match in VBA

不羁的心 提交于 2020-01-07 04:24:06
问题 I have a problem with my code for a Macro I am writing for Excel. It specifically relates to the Method Application.Match or alternatively Application.WorksheetFunction.Match I have an array of strings where I store the names of all companies in a list Dim CompanyID() As String ReDim CompanyID(NumCO) For i = 1 To NumCO CompanyID(i) = Worksheets("Sheet1").Cells(i, 1).Value Next i Then I creat a Collection to only contain all different companies Dim DifCO As New Collection, a On Error Resume

VBA UDF to split string array

删除回忆录丶 提交于 2020-01-07 04:18:40
问题 I've written this function which works, now I want to be able to call it as a worksheet function as in the pics, any guidance would be welcome: There are two parts to the array separated by a semi-colon. Function CellToRange(strDelimiter As String, Optional strColDelimiter As String) As String On Error GoTo CellToRange_Error Dim rnSource, rnDest As Range Dim i As Integer Dim Orig As Variant Dim txt As String Dim intPos As Integer Set rnSource = Range("F16") Set rnDest = Range("D21") txt =

Transpose from rows to column (VBA)

风流意气都作罢 提交于 2020-01-07 04:07:09
问题 My question is: how to transpose the last four rows of the column G into different columns? I usually use this static code: Worksheets("Sheet8").Range("A1:A5").Copy Worksheets("Sheet9").Range("A1").PasteSpecial Transpose:=True But it doesn't allow me to stay on the same Sheet. So I am trying to combine it with this code: Dim r As Range, N As Long N = Cells(Rows.Count, "A").End(xlUp).Row Set r = Cells(N, 1).EntireRow r.Copy Cells(N + 1, 1).PasteSpecial Transpose:=True r.Delete Before After Any

Unprotect sheet/Workbook Excel VBA

南笙酒味 提交于 2020-01-07 04:01:07
问题 Sub VBA_Read_External_Workbook() ' Get customer workbook... Dim customerBook As Workbook Dim filter As String Dim caption As String Dim customerFilename As String Dim customerWorkbook As Workbook Dim targetWorkbook As Workbook Dim sheet As String ' make weak assumption that active workbook is the target Set targetWorkbook = Application.ActiveWorkbook ' get the customer workbook filter = "Text files (*.xlsb),*.xlsb" caption = "Please Select an input file " customerFilename = Application

Check boxes in user form to select sheets to print

时光总嘲笑我的痴心妄想 提交于 2020-01-07 03:53:21
问题 I have code that selects an array of sheets to be printed to a pdf document, however I am trying to implement a user form with a series of check boxes that correspond to a specific sheets. The following code selects a predetermined array of sheets and prints them as a pdf Sub PDFAllSheets_Click() Dim ws As Worksheet Dim strPath As String Dim myfile As Variant Dim strFile As String Dim sheetstoprint As String On Error GoTo errHandler Set ws = ActiveSheet strFile = "E_CALC_" & Worksheets(

VBA Copy Whole Column From SQL Recordset

风流意气都作罢 提交于 2020-01-07 03:53:11
问题 I have a fixed data set of 6 rows being queried in SQL. I want to transfer the data in this query into Excel. My question is in two parts: 1 - Can I pull back multiple columns in one go or do I have to do it column by column? I have 17 columns and 6 rows. As this is transferred into my Excel template I'm looking to break this up into thirds so columns 1 to 7 go in one space 8 to 11 in another section and 12 to 17 in another section in Excel. 2 - When using my current code of: Range("F2")

I need to insert data into SQL server from Excel using VBA

怎甘沉沦 提交于 2020-01-07 03:53:08
问题 I need to insert test-vba.xlsx data into SQL server to the particular database Sub insertion() Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim sConnString As String Dim rsstring As String Dim m, nrows As Integer Set wkb = Workbooks("test-vba.xlsx").Worksheets("Sheet1").Activate sConnString = "Provider=SQLOLEDB;Data Source=PRATEEP-PC\SQLEXPRESS;" & _ "Initial Catalog=PPDS_07Dec_V1_Decomposition;" & _ "Integrated Security=SSPI;" Set conn = New ADODB.Connection Set rs = New ADODB

Excel VBA: Failed method on pivot table

这一生的挚爱 提交于 2020-01-07 03:50:10
问题 I am creating a VBA macro. Among other things, it makes a pivot table: With Worksheets("Working") lRow = .Range("A" & .Rows.Count).End(xlUp).Row lColumn = .Range("A" & .Columns.Count).End(xlToLeft).Column ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=.Range(.Cells(1, 1), .Cells(lRow, lColumn))).CreatePivotTable TableDestination:="", TableName:="HDPivotTable", DefaultVersion:=xlPivotTableVersion10 End With ActiveSheet.Name = "HD Pivot" ActiveSheet.PivotTableWizard

Enter number into a cell and search an array for that number using a 2D range

那年仲夏 提交于 2020-01-07 03:49:20
问题 I am reconciling accounts and was wondering if there is a formula/VBA I can use to search for certain values, as the find function doesn't work? For example: enter -54.12 into a cell, formula/VBA searches an array for that number. This may explain better what I'm trying to achieve, albeit more of an explanation than coding: Sub Test() Dim Value As Integer Dim Account As Range Dim Lookup As Boolean Value = Range("D8") Account = Range("E:E") Set Value = A Set Account = B ' IF A is present in B