多个单元格的文本功能

时间:2015-07-27 12:15:16

标签: vba excel-vba excel

我想知道在多个单元格上使用文本函数是否有替代方法(而不是循环)。

例如,下面的代码可以正常用于查找

Range("c2:c6").Value = Application.WorksheetFunction.VLookup(Range("a2:a6"), Range("a2:b6"), 2, 0)

但是我的文字函数

出现以下错误
Range("c2:c6").Value = Application.WorksheetFunction.Text(Range("A2:A6"), "000000")

4 个答案:

答案 0 :(得分:4)

你想要做的就是格式化单元格并复制值?

如下所示?

    With Range("C2:C6")
        .Value = Range("A2:A6").Value
        .NumberFormat = "000000"
    End With

宏录制器给了我类似于上面的一些优化

使用公式

Range("C2").FormulaR1C1 = "=TEXT(RC[-2],""000000"")"
Range("C3").FormulaR1C1 = "=TEXT(RC[-2],""000000"")"
Range("C4").FormulaR1C1 = "=TEXT(RC[-2],""000000"")"
Range("C5").FormulaR1C1 = "=TEXT(RC[-2],""000000"")"
Range("C6").FormulaR1C1 = "=TEXT(RC[-2],""000000"")"

或者更有活力的东西

For Each cell In Range("C2:C6")
    cell.FormulaR1C1 = "=TEXT(RC[-2],""000000"")"
Next

答案 1 :(得分:3)

一行代码

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:${catalina.home}/webapps/appName/configuration/jdbc.properties"/>

        <property name="location" value="file:${catalina.base}/webapps/appName/configuration/jdbc.properties"/>

<property name="location" value="/configuration/jdbc.properties"/>

<property name="location" value="file:/configuration/jdbc.properties"/>

<property name="location" value="classpath:/configuration/jdbc.properties"/>

<property name="location" value="classpath:/configuration/jdbc.properties"/>

</bean> 

如果您需要对此进行说明,请参阅This

答案 2 :(得分:2)

如果您真的想避免循环,可以使用Public Class ViewHelpContent Inherits System.Web.UI.Page 'Method that dynamically decides based on file extension which file viewer to render Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim embed As String = String.Empty Dim count As Integer Dim fileName As String = String.Empty Dim extension As String = String.Empty Try If Not IsPostBack Then If Not Request.QueryString("btnId") Is Nothing Then Dim btnId As String = Request.QueryString("btnId").ToString() count = GetFileAttachedToButton(btnId, fileName) extension = Path.GetExtension(fileName) If extension.ToLower() = ".pdf" Then embed = "<object id=""objPDFViewer"" data=""{0}{1}"" type=""application/pdf"">" embed &= "If you are unable to view file, you can download from <a href = ""{0}{1}&download=1"">here</a>" embed &= " or there is no file available to be viewed." embed &= "</object>" ElseIf extension.ToLower() = ".jpeg" OrElse extension.ToLower() = ".jpg" OrElse extension.ToLower() = ".gif" Then embed = "<img id=""objImgViewer"" src=""{0}{1}"" alt=""No file is available to be viewed."" />" Else ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('No help content uploaded for this button.');", True) Return End If helpContent.Text = String.Format(embed, ResolveUrl("~/ProcessHelpContent.ashx?btnId="), btnId) End If End If Catch ex As Exception ExceptionHandler.LogError(ex) JSHelper.ShowSystemError(Me) End Try End Sub End Class Public Class ProcessHelpContent Implements System.Web.IHttpHandler 'Sub that processes the help request and generates the requested content based on button Id Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim btnId As String = String.Empty Dim fileName As String = String.Empty Dim contentType As String = String.Empty Dim outputDir As String = String.Empty Dim count As Integer Dim strPath As FileInfo = Nothing Dim extension As String = String.Empty Try If Not context.Request.QueryString("btnId") Is Nothing Then btnId = context.Request.QueryString("btnId").ToString() outputDir = ConfigurationManager.AppSettings("HelpContentFilesFolder") count = GetFileAttachedToButton(btnId, fileName) strPath = New FileInfo(outputDir & fileName) If Not String.IsNullOrWhiteSpace(fileName) Then If File.Exists(strPath.FullName) Then extension = Path.GetExtension(strPath.FullName) If context.Request.QueryString("download") = "1" Then context.Response.AppendHeader("Content-Disposition", Convert.ToString("attachment; filename=") & fileName) End If context.Response.Cache.SetCacheability(HttpCacheability.NoCache) If extension.ToLower() = ".pdf" Then context.Response.ContentType = "application/pdf" ElseIf extension.ToLower() = ".jpg" OrElse extension.ToLower() = ".jpeg" Then context.Response.ContentType = "image/jpeg" ElseIf extension.ToLower() = ".gif" Then context.Response.ContentType = "image/gif" End If context.Response.TransmitFile(strPath.FullName) context.Response.Flush() Else context.Response.Write("<script>alert('Uploaded help file is missing. Please re-upload or contact administrator.'); window.close();</script>") End If Else context.Response.Write("<script>alert('Uploaded help file is missing. Please re-upload or contact administrator.'); window.close();</script>") End If End If Catch ex As Exception ExceptionHandler.LogError(ex) context.Response.Write("<script>alert('Uploaded help file is missing. Please re-upload or contact administrator.'); window.close();</script>") End Try End Sub ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property End Class

Evaluate

答案 3 :(得分:1)

可替换地:

Sub dural()
    Range("C1:C6").Formula = "=Text(A1,""000000"")"
End Sub